//判读对象是否响应某个消息
if( [Square instancesRespondToSelector: @selector(setSize:)] == YES ) {}
//根据给定的url获取字符串
NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncodingerror:&error];
我们在定义一个对象的时候,如果暂时不能给对象赋值,或者将对象的地址作为参数传递到别的方法里面,需要将此对象赋值为nil,这样可以避免"EXC_BAD_ACCESS"错误.
替换字符串
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement;
分割字符串
- (NSArray *)componentsSeparatedByString:(NSString *)separator;
//创建sqlite数据库,执行查询
NSString *dbName="/db.sqlite";
NSError *error;
NSFileManager *fileManager = [NSFileManagerdefaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingString:dbName];
// -- test to see if it exists
BOOL success = [fileManager fileExistsAtPath:writableDBPath];
// -- it not yet create,do it.
if(!success){
NSString *defaultDBPath = [[[NSBundlemainBundle] resourcePath] stringByAppendingString:dbName];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if(!success)
{
Assert(0, "Failed to create writable database file with message '%s'.", [error localizedDescription]);
}
}
//打开数据库
typedefunsigned int uint32;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *path = [documentDirectory stringByAppendingString:dbName];
sqlite3 *database;
if(sqlite3_open([path UTF8String], &database) == SQLITE_OK)
{
// Get the primary key
sqlite3_stmt *statement;
constchar *sql = "SELECT id,name,slogan,skips FROM skips WHERE pk=? and desc=?";
if(sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK)
{
//绑定查询参数
sqlite3_bind_int(init_statement, 1, key);
sqlite3_bind_text(insert_statement, 2, [sInfo.slogan UTF8String], -1, SQLITE_TRANSIENT);
// -- 循环,执行其他sql也使用这个语句
while(sqlite3_step(statement) == SQLITE_ROW){
//取出int型变量
uint32 stationID = sqlite3_column_int(statement, 0);
//取出字符串
NSString name = [NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 1)];
}
}
//重置查询语句,可以重新绑定参数
sqlite3_reset(statement);
//清除数据
sqlite3_finalize(statement);
//关闭数据库
sqlite3_close(database);
}
// 插入数据
// -- get the db
sqlite3 *database = [StationPlayerAppDelegateInstance].database;
if(insert_statement == nil)
{
staticchar *sql = "INSERT INTO favorites (id, name, slogan,orderNumber) VALUES (?, ?, ?,?)";
if(sqlite3_prepare_v2(database, sql, -1, &insert_statement, NULL) != SQLITE_OK)
{
Assert(0, "Failed to prepare insert statement for '%s'.", sqlite3_errmsg(database));
}
}
// -- prepare the values
sqlite3_bind_int(insert_statement, 4, sInfo.orderNumber);
sqlite3_bind_text(insert_statement, 3, [sInfo.sloganUTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insert_statement, 2, [sInfo.nameUTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_int(insert_statement, 1, sInfo.stationID);
int32 success = sqlite3_step(insert_statement);
sqlite3_reset(insert_statement);
if(success != SQLITE_ERROR)
{
// 取回插入数据的主键
sInfo.favoritesPrimaryKey = sqlite3_last_insert_rowid(database);
}
else
{
Assert(0, "Failed to insert into database with message '%s'.", sqlite3_errmsg(database));
}
// 隐藏navigation bar上的返回按钮,设置为自定义的按钮
[self.navigationItemsetHidesBackButton:YES];
UIBarButtonItem *left=[[UIBarButtonItemalloc]initWithTitle:NSLocalizedString(@"Back",@"") style:UIBarButtonItemStyleBorderedtarget:selfaction:@selector(backroot)];
self.navigationItem.leftBarButtonItem=left;