识别设备是否为pad
[[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad
判断设备方向为纵向
UIInterfaceOrientationIsPortrait() YES
NSUserDefault是存储在本地磁盘上的,相当于ini文件
[[NSUserDefaults standardUserDefaults] setObject:perid forKey:PerIdKey];
使用GCD实现多线程操作
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSArray* array = [myMngObjCtx getLastLogin];
//sub complete turn to main 子线程要自己管理内存释放
dispatch_async(dispatch_get_main_queue(), ^{
for ( NSManagedObject* obj in array) {
[self.userNameEd setText:[obj valueForKey:@"username"]];
}
});//main
}); //dispatch async
按钮图标总是显示为默认色实心圆,这样改代码
selImage =[selImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
如果是在cell里的button,则需要设置不透明
self.OrganNext.opaque = YES;
去除表格视图的表格线
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
服务端返回json的时候带有“null”
[myinfo.signature isKindOfClass:[NSNull class]]
如果写==[NSNull null]判断会有警告
从storyboard中读取资源生成controller
UIStoryboard* story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ViewController* login = [story instantiateViewControllerWithIdentifier:@"rlogin"];
获取app下document文件夹
[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
建立model、coordinate、sqlite文件关联
NSPersistentStoreCoordinator* coordin = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:aManaObjModel];
[coordin addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&err];
数据库删除
[aManaObjCtx deleteObject:managedobject];
BOOL ret = [aManaObjCtx save:&err];
数据库查询
NSEntityDescription* desc = [NSEntityDescription entityForName:@"Login" inManagedObjectContext:aManaObjCtx];
NSFetchRequest* req = [[NSFetchRequest alloc] initWithEntityName:@"Login"];
[req setEntity:desc];
NSSortDescriptor *sort=[[NSSortDescriptor alloc]initWithKey:@"lasttime" ascending:NO];
NSArray* sortArray = @[sort];
[req setSortDescriptors:sortArray];
NSPredicate* pre = [NSPredicate predicateWithFormat:@"personid = %@",personid];
[req setPredicate:pre];
NSError* err = nil;
NSArray* result = [aManaObjCtx executeFetchRequest:req error:&err];
alloc方法
类方法alloc申请到内存后,还顺便把内存全部置为0,这样就不必担心未初始化数据导致各种随机奇怪问题(比如C++语言会出现的问题)但是仍然必须调用对象的init方法(及其变体)
xml文档解析
-(BOOL)parser:(NSString*)string
{
//系统自带的
NSXMLParser *par = [[NSXMLParser alloc] initWithData:[string dataUsingEncoding:NSUTF8StringEncoding]];
[par setDelegate:self];//设置NSXMLParser对象的解析方法代理
return [par parse];//调用代理解析NSXMLParser对象,看解析是否成功
}
#pragma mark xmlparser
//step 1 :准备解析
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
//start node
NSLog(@"%@",NSStringFromSelector(_cmd) );
}
//step 2:准备解析节点 命名空间、限定名
(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)
elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary*)attributeDict
{
//
NSLog(@"ele:%@ \n nsp:%@ \n qN:%@ \n attr:%@",elementName,
namespaceURI,qName,[attributeDict description] );
}
//step 3:获取一个节点里的内容;可能会调用多次!接收时使用append
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
//content
NSLog(@"%@",string );
}
//step 4 :解析完当前节点
(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)
elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
NSLog(@"%@ end",elementName );
}
//step 5;解析结束
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
//document end
NSLog(@"%@",NSStringFromSelector(_cmd) );
}
//获取cdata块数据
- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock
{
NSLog(@"%@",CDATABlock.description );
}
把数组中元素用指定间隔符合成字符串
[array componentsJoinedByString:@”-”]
指定分隔符分割字符串到数组
[title componentsSeparatedByString:@"-"]
从文件读取内容得到字符串
[[NSString alloc] initWithContentsOfFile:@"a.txt" encoding:NSUTF8StringEncoding error:nil];
测试一个类是否有某个方法实现,一般用于接口类
[model respondsToSelector:@selector(selector)];
KVC快速运算,成员变量名不要打错,返回值为对象
NSNumber* count = [garage valueForKeyPath:@"cars.@count" ];
NSNumber* sum = [garage valueForKeyPath:@"[email protected]"];
NSNumber* avg = [garage valueForKeyPath:@"[email protected]"];
block代码块
app启动