首页 文章 精选 留言 我的

精选列表

搜索[网站开发],共10000篇文章
优秀的个人博客,低调大师

Android -- NDK开发入门

第一步,建立一个普通的Android项目HelloNDK,然后在与src同一级的目录下新建一个jni目录; 第二步,在jni目录下新建一个hello_ndk.c文件,代码如下: #include <string.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/ioctl.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <jni.h> #include <android/log.h> /********获取字符串************* */ jstring Java_com_example_hellondk_MainActivity_readJNIString( JNIEnv* env, jobject obj) { return (*env)->NewStringUTF(env, "Hello from JNI --- 22222 !"); } 说明如下: Java_com_example_hellondk_MainActivity_readJNIString // 方法名,由三部分组成,Java + [com_example_hellondk_HelloNDKActivity](包名+activity名) + [readJNIString](在java代码中调用的方法名) (*env)->NewStringUTF(env, "Hello from JNI --- 22222 !"); //在java中接收的返回值,不能直接return,一定要调用(*env)->NewStringUTF()这个方法返回 第三步,在jni目录下新建一个Android.mk文件,代码如下: LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := hello_ndk LOCAL_SRC_FILES := hello_ndk.c LOCAL_LDLIBS := -llog include $(BUILD_SHARED_LIBRARY) 说明如下: LOCAL_MODULE := hello_ndk //这个与生成.so文件名有关,和导入Native Support的.so文件有关 LOCAL_SRC_FILES := hello_ndk.c //这个是读取相关的C文件名 第四步,配置android项目的native support,选中项目右键Android Tools--Add Native Support, 打开Add Android Native Support对话框,输入第三步中配置的LOACL_MODULE的值,如下图: 第五步,修改MainActivity.java文件,代码如下: package com.example.hellondk; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { TextView mTestTv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTestTv = (TextView)findViewById(R.id.test_tv); mTestTv.setText(readJNIString()); } private native String readJNIString(); static { System.loadLibrary("hello_ndk");//引入hello_ndk.so } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } 说明如下: 项目运行的时候首先会执行static里面的方法,引入hello_ndk.so,然后执行onCreate里的方法,读取C文件里的方法获取返回值并显示在TextView控件上。 第六步,运行Android Application项目,程序会首先生成.so文件,成功后继续运行android项目,生成.so的过程如下: 我是天王盖地虎的分割线 本文转自我爱物联网博客园博客,原文链接:http://www.cnblogs.com/yydcdut/p/3908734.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

IOS开发博客学习

M了个J :http://www.cnblogs.com/mjios/tag/objective-c/ http://www.cnblogs.com/tianjian/p/3358602.html Cocos2d-x VS环境配置 泰然论坛:http://www.ityran.com/archives/category/cocos2d-iphone 1.http://www.cnblogs.com/zilongshanren/archive/2011/09/19/2181558.html 非常适用的入门教程 2.http://www.cnblogs.com/BigPolarBear/tag/iOS/ 3.一些相应的环境配置,以及简单语法介绍,适合入门 http://blog.csdn.net/xiaominghimi/article/details/6937685 http://blog.csdn.net/sdhjob/article/details/8173176 沈大海 4.Cocos2d-x的使用 http://cocos2d.cocoachina.com/ http://cocos2d.cocoachina.com/resource 重要的博客http://blog.csdn.net/honghaier/article/details/7887873 Cocos2d-x视频教程 http://www.tudou.com/plcover/Yofz8yOj62c/ 5.http://www.cnblogs.com/zilongshanren/archive/2012/02/17/2356516.htm http://www.cnblogs.com/zilongshanren/tag/cocos2d/ 8.http://blog.csdn.net/honghaier/article/details/7887873重点 OC知识点 1.http://rzchina.blog.163.com/blog/#m=0&t=1&c=fks_084069084082083074087082094095085094080066084083087074083 本文转自夏雪冬日博客园博客,原文链接:http://www.cnblogs.com/heyonggang/p/3351269.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

iOS开发-状态模式

状态模式允许对象内部状态改变时改变它的行为,对象看起来好像修改了它的类。状态模式看起来和策略模式比较相像,策略模式是将可以互换的行为封装起来,然后通过使用委托的方式,决定使用哪一个行为,状态也是封装行为,不同的是可以将行为委托到当前状态。一个需要从外部设置,一个是内部通过状态变更达到行为变成的目的。 基础知识 状态模式的UML类图: State封装基本的状态行为,我们通过Cotext上下文持有状态子类的实例,外部发起请求,我们就可以委托状态进行处理。地铁里面一般都有自动饮料售卖机,我们将所有的饮料都当做商品来看,我们如果需要买饮料需要两步,投币,取货,如果没有商品还可以退钱三种行为,关于自动售卖机的存在无钱状态,有钱状态,售卖中状态,已售罄状态四种状态,稍微思考一下,可以看下面的具体实现。 设计实战 状态行为基类: @protocol StateProtocol <NSObject> @optional -(void)putMoney; @optional -(void)ejectMoney; @optional -(void)pressButton; @optional -(void)distribute; @end @interface State : NSObject<StateProtocol> @end 售卖机处于无钱状态(NoMoneyState): @protocol NoMoneyDelegate <NSObject> -(void)setCurrentState:(State *)currentState; -(State *)geHasMoneyState; @end //没钱状态 @interface NoMoneyState : State @property (weak,nonatomic) id<NoMoneyDelegate> delegate; @end @implementation NoMoneyState -(void)putMoney{ NSLog(@"NoMoneyState-putMoney:投放钱币"); [self.delegate setCurrentState:[self.delegate geHasMoneyState]]; } -(void)ejectMoney{ NSLog(@"NoMoneyState-ejectMoney:没有投入钱币,无法退钱"); } -(void)pressButton{ NSLog(@"NoMoneyState-pressButton:请先投币"); } -(void)distribute{ NSLog(@"NoMoneyState-pressButton:请投币"); } @end 售卖机处于有钱状态(HasMoneyState): @protocol HasMoneyDelegate <NSObject> -(void)setCurrentState:(State *)currentState; -(State *)getNoMoneyState; -(State *)getSoldState; @end //有钱状态 @interface HasMoneyState : State @property (weak,nonatomic) id<HasMoneyDelegate> delegate; @end @implementation HasMoneyState -(void)putMoney{ NSLog(@"HasMoneyState-putMoney:已经投入了钱,暂不支持投入"); } -(void)ejectMoney{ NSLog(@"HasMoneyState-ejectMoney:退钱,重新设置售卖机为无前状态"); [self.delegate setCurrentState:[self.delegate getNoMoneyState]]; } -(void)pressButton{ NSLog(@"HasMoneyState-pressButton:按钮按下,取货"); [self.delegate setCurrentState:[self.delegate getSoldState]]; } -(void)distribute{ NSLog(@"HasMoneyState-distribute:无法进行取出商品"); } @end 售卖机处于售卖状态: @protocol SoldDelegate <NSObject> -(void)setCurrentState:(State *)currentState; -(void)realseProduct; -(NSInteger)getCurrentCount; -(State *)getNoMoneyState; -(State *)getSoldOutState; @end //售出状态 @interface SoldState : State @property (weak,nonatomic) id<SoldDelegate> delegate; @end @implementation SoldState -(void)putMoney{ NSLog(@"SoldState-putMoney:请稍后,正在进行商品出售"); } -(void)ejectMoney{ NSLog(@"SoldState-putMoney:请稍后,正在进行商品出售,无法退钱"); } -(void)pressButton{ NSLog(@"SoldState-putMoney:请在取出物品之后重新投币"); } -(void)distribute{ [self.delegate realseProduct]; if ([self.delegate getCurrentCount]) { [self.delegate setCurrentState:[self.delegate getNoMoneyState]]; }else{ [self.delegate setCurrentState:[self.delegate getSoldOutState]]; } } @end 售罄状态(SoldOutState): @protocol SoldOutDelegate <NSObject> -(void)setCurrentState:(State *)currentState; @end //售罄状态 @interface SoldOutState : State @property (weak,nonatomic) id<SoldOutDelegate> delegate; @end @implementation SoldOutState -(void)putMoney{ NSLog(@"SoldOutState-PutMoney:已售罄"); } -(void)ejectMoney{ NSLog(@"SoldOutState-ejectMoney:无法退钱"); } -(void)pressButton{ NSLog(@"SoldOutState-pressButton:无法售出"); } -(void)distribute{ NSLog(@"SoldOutState-distribute:无法分发"); } @end 售卖机(SaleMachine): @interface SaleMachine : NSObject<NoMoneyDelegate,HasMoneyDelegate,SoldOutDelegate,SoldDelegate> @property (strong,nonatomic) NoMoneyState *noMoneyState; @property (strong,nonatomic) HasMoneyState *hasMoneyState; @property (strong,nonatomic) SoldOutState *soldOutState; @property (strong,nonatomic) SoldState *soldState; -(instancetype)initWithCount:(NSInteger)count; -(void)setCurrentState:(State *)currentState; -(void)putMoney; -(void)ejectMoney; -(void)pressButton; @end @interface SaleMachine() @property (strong,nonatomic) State *state; @property (assign,nonatomic) NSInteger productCount; @end @implementation SaleMachine -(instancetype)initWithCount:(NSInteger)count{ self=[super init]; if (self) { self.noMoneyState=[[NoMoneyState alloc]init]; self.noMoneyState.delegate=self; self.hasMoneyState=[[HasMoneyState alloc]init]; self.hasMoneyState.delegate=self; self.soldState=[[SoldState alloc]init]; self.soldState.delegate=self; self.soldOutState=[[SoldOutState alloc]init]; self.soldOutState.delegate=self; self.productCount=count; if (count) { self.state=self.noMoneyState; } } return self; } -(void)putMoney{ [self.state putMoney]; } -(void)ejectMoney{ [self.state ejectMoney]; } -(void)pressButton{ [self.state pressButton]; [self.state distribute]; } -(void)setCurrentState:(State *)currentState{ self.state=currentState; } #pragma mark - NoMoenyDelegate -(State *)geHasMoneyState{ return self.hasMoneyState; } #pragma mark - HasMoneyDelegate -(State *)getNoMoneyState{ return self.noMoneyState; } -(State *)getSoldState{ return self.soldState; } #pragma mark - SoldDelegate -(void)realseProduct{ NSLog(@"SoldDelegate-realseProduct:商品售出"); if (self.productCount) { self.productCount-=1; } } -(State *)getSoldOutState{ return self.soldOutState; } -(NSInteger)getCurrentCount{ return self.productCount; } @end 测试: SaleMachine *machine=[[SaleMachine alloc]initWithCount:1]; [machine putMoney]; [machine ejectMoney]; [machine putMoney]; [machine pressButton]; SaleMachine *next=[[SaleMachine alloc]initWithCount:1]; [next putMoney]; [next ejectMoney]; 测试效果: 状态模式的优缺点: 优点:状态模式允许一个对象基于内部状态有不同的行为,将行为委托给状态对象执行,状态转化可以由Context也可以由状态行为控制,比较灵活; 缺点:状态模式的使用必然会增加系统类和对象的个数。状态模式的结构与实现都较为复杂,如果使用不当将导致程序结构和代码的混乱。 本文转自Fly_Elephant博客园博客,原文链接:http://www.cnblogs.com/xiaofeixiang/p/5135479.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

iOS开发-automaticallyAdjustsScrollViewInsets属性

最近遇到一个问题是这样的,App一般自己都会有一个UINavigationController,顶部TableView如果有tableHeaderView如果设置起始位置是(0,0)是在导航栏的下面的,为了更好地UI希望从屏幕的(0,0)开始,就遇到了上面的这个问题,简单的看一下效果: 主要代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 - (UITableView *)tableView { if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth( self .view.bounds), CGRectGetHeight( self .view.bounds)) style:UITableViewStylePlain]; // _tableView.tableFooterView = [[UIView alloc] init]; _tableView.rowHeight =40.0; _tableView.sectionHeaderHeight = 0.0; _tableView.sectionFooterHeight = 0.0; _tableView.dataSource = self ; _tableView.delegate = self ; } return _tableView; } -( NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:( NSInteger )section{ return 5; } //http://www.cnblogs.com/xiaofeixiang 技术交流:228407086 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath{ UITableViewCell *cell=[[UITableViewCell alloc]init]; cell.textLabel.text=@ "博客园-FlyElephant" ; return cell; } 这个时候设置automaticallyAdjustsScrollViewInsets效果如下: 官方文档解释如下: 1 2 3 4 5 6 7 8 9 10 11 12 A Boolean value that indicates whether the view controller should automatically adjust its scroll view insets. Declaration SWIFT var automaticallyAdjustsScrollViewInsets: Bool OBJECTIVE-C @property ( nonatomic , assign) BOOL automaticallyAdjustsScrollViewInsets Discussion The default value of this property is YES , which allows the view controller to adjust its scroll view insets in response to the screen areas consumed by the status bar, navigation bar, and toolbar or tab bar. Set to NO if you want to manage scroll view inset adjustments yourself, such as when there is more than one scroll view in the view hierarchy. Availability Available in iOS 7.0 and later. 简单点说就是automaticallyAdjustsScrollViewInsets根据按所在界面的status bar,navigationbar,与tabbar的高度,自动调整scrollview的 inset,设置为no,不让viewController调整,我们自己修改布局即可~ 本文转自Fly_Elephant博客园博客,原文链接:http://www.cnblogs.com/xiaofeixiang/p/4655932.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

iOS开发-装饰模式

装饰模式是指在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。通过创建一个包装对象,也就是装饰来包裹真实的对象。装饰模式中的装饰对象和真实对象有相同的接口。这样客户端对象就能以和真实对象相同的方式和装饰对象交互,同时装饰对象包含一个真实对象的引用(reference),装饰对象接受所有来自客户端的请求。它把这些请求转发给真实的对象。 装饰模式与继承都可以要扩展对象的功能,但是装饰模式可以提供比继承更多的灵活性。通过使用不同的具体装饰类以及这些装饰类的排列组合,可以创造出很多不同行为的组合。 基础设计 单纯的看概念有点单调,看一张经典的装饰模式的UML类图: Component:定义ConcreteComponent和Decorator类要实现的方法,装饰对象和真实对象的之间的通信就是通过Component实现; ConcreteComponent:真实对象,使用ConcreteComponent的派生类提供核心功能,与Decorator是同一级别; Decorator:具有特定装饰功能的类,用来装饰ConcreteComponent类,具体的装饰子类通过继承Decorator实现; 关于整体的轮廓有了一个大概的了解,我们可以通过生活的例子来模拟装饰模式,关于房子,有商业住宅和民用住宅,买了房子我们需要装修,桌子,椅子必不可少,我们最终需要算一下总费用,思考几秒可以看下面具体实现; 功能实现 设计基础类House和协议: 1 2 3 4 5 6 7 8 9 10 11 12 @protocol HouseDelegate <NSObject> @optional -( double )totalMoney; @end @ interface House : NSObject<HouseDelegate> -(NSString *)detialInfo; @end House的子类CommercialHouse,被装饰对象: 1 2 3 4 5 6 7 8 9 10 11 @implementation CommercialHouse -( double )totalMoney{ return 360000.89; } -(NSString *)detialInfo{ return [NSString stringWithFormat: @"商业住宅" ]; } @end 装饰对象的基类HouseDecorator,这里需要保持一个对被装饰对象的引用: 1 2 3 4 5 6 7 @ interface HouseDecorator : House -(instancetype)initWithHouse:(House *)house; @property (strong,nonatomic) House *house; @end 1 2 3 4 5 6 7 8 9 10 @implementation HouseDecorator -(instancetype)initWithHouse:(House *)house{ self=[super init]; if (self) { self.house=house; } return self; } @end 装饰对象TableDecorator: 1 2 3 4 5 6 7 8 9 10 11 @implementation TableDecorator -( double )totalMoney{ return self.house.totalMoney+10; } -(NSString *)detialInfo{ return [NSString stringWithFormat: @"%@--桌子" ,self.house.detialInfo]; } @end 装饰对象ChairDecorator: 1 2 3 4 5 6 7 8 9 10 11 @implementation ChairDecorator -( double )totalMoney{ return self.house.totalMoney+100; } -(NSString *)detialInfo{ return [NSString stringWithFormat: @"%@--椅子" ,self.house.detialInfo]; } @end 回到最初的总费用问题,我们看下结果: 1 2 3 4 5 6 7 House *house=[[CommercialHouse alloc]init]; house=[[TableDecorator alloc]initWithHouse:house]; house=[[ChairDecorator alloc]initWithHouse:house]; NSLog( @"房子价格:%f" , [house totalMoney]); NSLog( @"房子详情:%@" ,[house detialInfo]); NSLog( @"博客园-FlyElephant" ); NSLog( @"http://www.cnblogs.com/xiaofeixiang/" ); 桌子椅子的价格是可以动态变化的,桌子椅子的数量的也是不确定的,从这些角度看例子稍微有点勉强,不过大概功能设计实现大同小异,万变不离其宗,相信大家会对装饰模式有自己独到的见解~ 本文转自Fly_Elephant博客园博客,原文链接:http://www.cnblogs.com/xiaofeixiang/p/5097972.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

iOS开发-策略模式

策略(Strategy)模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换。策略模式让算法独立于使用它的客户而独立变化。策略模式是对算法的包装,是把使用算法的责任和算法本身分割开来,委派给不同的对象管理。看到策略模式的时候有的时候跟简单工厂相比较,其实有很大的迷惑性,都是继承多态感觉没有太大的差异性,简单工厂模式是对对象的管理,策略模式是对行为的封装。可以先简单的看一下结构图: 之前简单工厂是通过银行卡作为例子的简单工厂将不同的银行卡抽象出来,如果在策略模式中我们可以将每张银行卡的购物,吃饭,住房。。作为一个简单的消费策略抽象出来,也可以以操作系统类比,Windows,OS X,Linux可以作为简单的对象抽象,系统中都是有默认软件的,我们不需要管软件的安装,如果没有软件的话我们就需要自己下载,可以将软件的安装作为一个策略封装起来。 Strategy的抽象类: 1 2 3 4 5 @ interface SoftWareStrategy : NSObject -( void )installStrategy; @end 继承Strategy的Xcode的策略类: 1 2 3 4 5 6 7 @implementation XcodeStrategy -( void )installStrategy{ NSLog( @"Xcode安装成功" ); } @end 继承Strategy的QQ的策略类: 1 2 3 4 5 6 7 8 @implementation QQStrategy -( void )installStrategy{ NSLog( @"QQ安装成功" ); NSLog( @"原文地址:http://www.cnblogs.com/xiaofeixiang" ); } @end Context类: 1 2 3 4 5 6 7 8 9 10 11 12 typedef NS_OPTIONS(NSInteger, StrategyType){ StrategyXcode, strategyQQ }; @ interface SoftWareContext : NSObject -(instancetype)initWithStrategyType:(StrategyType)strategyType; -( void )installResult; @end Context的实现: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 @ interface SoftWareContext() @property (strong,nonatomic) SoftWareStrategy *strategy; @end @implementation SoftWareContext -(instancetype)initWithStrategyType:(StrategyType)strategyType{ self=[super init]; if (self) { switch (strategyType) { case StrategyXcode: self.strategy=[[XcodeStrategy alloc]init]; break ; case strategyQQ: self.strategy=[[QQStrategy alloc]init]; break ; } } return self; } -( void )installResult{ [self.strategy installStrategy]; } @end 最终调用: 1 2 SoftWareContext *context=[[SoftWareContext alloc]initWithStrategyType:StrategyXcode]; [context installResult]; 这里有三个概念再看一下应该就清晰多了: 环境(Context)角色:持有一个Strategy的引用; 抽象策略(Strategy)角色:这是一个抽象角色,通常由一个接口或抽象类实现。此角色给出所有的具体策略类所需的接口; 具体策略(ConcreteStrategy)角色:包装了相关的算法或行为; 本文转自Fly_Elephant博客园博客,原文链接:http://www.cnblogs.com/xiaofeixiang/p/4719566.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

iOS开发之FMDB

sqlite作为一个轻量级的 数据库,由于它占用的内存很少,因此在很多的嵌入式设备中得到广泛的使用。iOS的SDK很早就开始支持了SQLite,我们只需要加入 libsqlite3.dylib 以及引入 sqlite3.h 头文件即可,但由于原生sqlite的API不是很友好,因此使用的话一般会对其做一层封装,其中以开源的FMDB最为流行。 FMDB主要的类 1.FMDatabase – 表示一个单独的SQLite数据库。 用来执行SQLite的命令。 2.FMResultSet – 表示FMDatabase执行查询后结果集 3.FMDatabaseQueue – 当你在多线程中执行操作,使用该类能确保线程安全。 FMDB的使用 数据库的创建: 创建FMDatabase对象时需要参数为SQLite数据库文件路径。该路径可以是以下三种之一: 1..文件路径。该文件路径无需真实存,如果不存在会自动创建。 2..空字符串(@”")。表示会在临时目录创建一个临时数据库,当FMDatabase 链接关闭时,文件也被删除。 3.NULL. 将创建一个内存数据库。同样的,当FMDatabase连接关闭时,数据会被销毁。 内存数据库: 通常数据库是存放在磁盘当中的。然而我们也可以让存放在内存当中的数据库,内存数据库的优点是对其操作的速度较快,毕竟访问内存的耗时低于访问磁盘,但内存数据库有如下缺陷:由于内存数据库没有被持久化,当该数据库被关闭后就会立刻消失,断电或程序崩溃都会导致数据丢失;不支持读写互斥处理,需要自己手动添加锁;无法被别的进程访问。 临时数据库: 临时数据库和内存数据库非常相似,两个数据库连接创建的临时数据库也是各自独立的,在连接关闭后,临时数据库将自动消失,其底层文件也将被自动删除。尽管磁盘文件被创建用于存储临时数据库中的数据信息,但是实际上临时数据库也会和内存数据库一样通常驻留在内存中,唯一不同的是,当临时数据库中数据量过大时,SQLite为了保证有更多的内存可用于其它操作,因此会将临时数据库中的部分数据写到磁盘文件中,而内存数据库则始终会将数据存放在内存中。 创建数据库:FMDatabase *db= [FMDatabase databaseWithPath:dbPath] ; 在进行数据库的操作之前,必须要先把数据库打开,如果资源或权限不足无法打开或创建数据库,都会导致打开失败。 如下为创建和打开数据库的示例: NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentDirectory = [paths objectAtIndex:0]; //dbPath: 数据库路径,存放在Document中。 NSString *dbPath = [documentDirectory stringByAppendingPathComponent:@"MYTest.db"]; //创建数据库实例 db 这里说明下:如果路径中不存在"MYTest.db"的文件,sqlite会自动创建"MYTest.db" FMDatabase *db= [FMDatabase databaseWithPath:dbPath] ; if (![db open]) { NSLog(@"Could not open db."); return ; } 更新操作 一切不是SELECT命令都视为更新操作,包括CREATE, UPDATE, INSERT,ALTER,COMMIT, BEGIN, DETACH, DELETE, DROP, END, EXPLAIN, VACUUM和REPLACE等。 创建表: [db executeUpdate:@"CREATE TABLE myTable (Name text,Age integer)"]; 插入 [db executeUpdate:@"INSERT INTO myTable (Name,Age) VALUES (?,?)",@"jason",[NSNumber numberWithInt:20]]; 更新 [db executeUpdate:@"UPDATE myTable SET Name = ? WHERE Name = ? ",@"john",@"jason"];. 删除 [db executeUpdate:@"DELETE FROM myTable WHERE Name = ?",@"jason"]; 查询操作 SELECT命令就是查询,执行查询的方法是以 -excuteQuery开头的。执行查询时,如果成功返回FMResultSet对象, 错误返回nil. 读取信息的时候需要用while循环: FMResultSet *s = [db executeQuery:@"SELECT * FROM myTable"]; while ([s next]) { //从每条记录中提取信息 } 关闭数据库 当使用完数据库,你应该 -close 来关闭数据库连接来释放SQLite使用的资源。 [db close]; 参数 通常情况下,你可以按照标准的 SQL语句,用?表示执行语句的参数,如: INSERT INTO myTable VALUES (?, ?, ?) 然后,可以我们可以调用executeUpdate方法来将?所指代的具体参数传入,通常是用变长参数来传递进去的,如下: NSString *sql = @"insert into myTable (name, password) values (?, ?)"; [db executeUpdate:sql, user.name, user.password]; 这里需要注意的是,参数必须是NSObject的子类,所以象int,double,bool这种基本类型,需要进行相应的封装 [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:42]]; 多线程操作 由于FMDatabase对象本身不是线程安全的,因此为了避免在多线程操作的时候出错,需要使用 FMDatabaseQueue来执行相关操作。只需要利用一个数据库文件地址来初使化FMDatabaseQueue,然后传入一个block到inDatabase中,即使是多个线程同时操作,该queue也会确保这些操作能按序进行,保证线程安全。 创建队列: FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:aPath]; 使用方法: [queue inDatabase:^(FMDatabase *db) { [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:1]]; [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:2]]; FMResultSet *rs = [db executeQuery:@"select * from foo"]; while([rs next]) { … } }]; 至于事务可以像这样处理: [queue inTransaction:^(FMDatabase *db, BOOL *rollback) { [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:1]]; [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:2]]; [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:3]]; if (somethingWrongHappened) { *rollback = YES; return; } // etc… [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:4]]; }]; 最新内容请见作者的GitHub页:http://qaseven.github.io/

优秀的个人博客,低调大师

iOS开发-Bug锦囊

duplicate symbols for architecture armv7 今天修改了自己项目的部分代码,发现XCode编译的时候报错:duplicate symbols for architecture armv7 1.排查是否有名字重复的文件; 2.检查是否在#import头文件的时候,不小心将.h写成了.m(这种情况居多,本人属于此类情况) No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=x86_64, VALID_ARCHS=i386 App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file. xcode7中苹果不允许使用http协议,需要使用Https协议,如果需要使用http协议,需要在infoList中设置NSAppTransportSecurity: <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict> 本文转自Fly_Elephant博客园博客,原文链接:http://www.cnblogs.com/xiaofeixiang/p/4528097.html,如需转载请自行联系原作者

资源下载

更多资源
腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册