ios nsstring怎么从后面开始截取字符串

ios nsstring怎么从后面开始截取字符串,第1张

//有两种方法

NSString *originalString = @"I wanna go to the Rio Olympics"

//1.substringFromIndex: 从第18位字符开始到最后的字符的字符串截取

NSString *subfixString = [originalString substringFromIndex:18]

//2.substringWithRange: 从第18位字符开始,长度为(总长度-18)的长度的字符串载取

NSString *rangeString = [originalString substringWithRange:NSMakeRange(18, originalString.length-18)]

NSLog(@"sub=%@,\nran=%@",subfixString,rangeString)

//输出结果为:

//--2016-08-09 10:33:06.764 Objective-c-Practise[99217:10851975] sub=Rio Olympics,

//--ran=Rio Olympics

iPhone中数据库使用方法是本文要介绍的内容,直接进入话题介绍,iPhone 中使用名为 SQLite 的数据库管理系统。它是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,

在嵌入式设备中,可能只需要几百K的内存就够了。它能够支持Windows/Linux/Unix等等主流的 *** 作系统,同时能够跟很多程序语言相结合,比如 Tcl、PHP、Java 等,还有 ODBC 接口,同样比起 Mysql、PostgreSQL 这两款开源世界著名的数据库管理系统来讲,它的处理速度比他们都快。

其使用步骤大致分为以下几步:

1. 创建DB文件和表格

2. 添加必须的库文件(FMDB for iPhone, libsqlite3.0.dylib)

3. 通过 FMDB 的方法使用 SQLite

创建DB文件和表格

$ sqlite3 sample.db sqlite>CREATE TABLE TEST( ...> id INTEGER PRIMARY KEY, ...> name VARCHAR(255) ...>)

简单地使用上面的语句生成数据库文件后,用一个图形化SQLite管理工具,比如 Lita 来管理还是很方便的。

然后将文件(sample.db)添加到工程中。

添加必须的库文件(FMDB for iPhone, libsqlite3.0.dylib)

首先添加 Apple 提供的 sqlite *** 作用程序库 ibsqlite3.0.dylib 到工程中。位置如下

/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS${VER}.sdk/usr/lib/libsqlite3.0.dylib

这样一来就可以访问数据库了,但是为了更加方便的 *** 作数据库,这里使用 FMDB for iPhone。

svn co http://flycode.googlecode.com/svn/trunk/fmdb fmdb

如上下载该库,并将以下文件添加到工程文件中:

FMDatabase.h FMDatabase.m FMDatabaseAdditions.h FMDatabaseAdditions.m FMResultSet.h FMResultSet.m

通过 FMDB 的方法使用 SQLite

使用 SQL *** 作数据库的代码在程序库的 fmdb.m 文件中大部分都列出了、只是连接数据库文件的时候需要注意 — 执行的时候,参照的数据库路径位于 Document 目录下,之前把刚才的 sample.db 文件拷贝过去就好了。

位置如下

/Users/xxxx/Library/Application Support/iPhone Simulator/User/Applications/xxxx/Documents/sample.db

BOOL success NSError *error NSFileManager *fm = [NSFileManager defaultManager] NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) NSString *documentsDirectory = [paths objectAtIndex:0] NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"sample.db"] success = [fm fileExistsAtPath:writableDBPath] if(!success){NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"sample.db"] success = [fm copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error] if(!success){ NSLog([error localizedDescription]) } } // 连接DB FMDatabase* db = [FMDatabase databaseWithPath:writableDBPath] if ([db open]) {[db setShouldCacheStatements:YES]// INSERT[db beginTransaction] int i = 0 while (i++ <20) { [db executeUpdate:@"INSERT INTO TEST (name) values (?)" , [NSString stringWithFormat:@"number %d", i]] if ([db hadError]) {NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]) }}[db commit] // SELECTFMResultSet *rs = [db executeQuery:@"SELECT * FROM TEST"] while ([rs next]) { NSLog(@"%d %@", [rs intForColumn:@"id"], [rs stringForColumn:@"name"]) }[rs close] [db close] }else{NSLog(@"Could not open db.") }


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/9560737.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-29
下一篇 2023-04-29

发表评论

登录后才能评论

评论列表(0条)

保存