IOS APP 开发中遇到的问题总结

1.  使用UIBarButtonItem 自定义图片时出现一个蓝色原点?

如下写法: 


UIBarButtonItem*commitBarItem= [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"confirm_submit"]  style:UIBarButtonItemStylePlain target:self action:@selector(commitAlarmListToServer)];


解决办法:(http://www.cocoachina.com/bbs/read.php?tid=180226)

UIImage*selectedImage=[UIImage imageNamed:@"confirm_submit"];

selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

UIBarButtonItem*commitBarItem= [[UIBarButtonItem alloc]initWithImage:selectedImage style:UIBarButtonItemStylePlain target:self action:@selector(commitAlarmListToServer)];

self.navigationItem.rightBarButtonItem= commitBarItem;

2.  NSArray/NSDictionary 数组或字典 转成json

(1) NSDictionary 转成NSString json

+ (NSString *)dictToJsonString:(NSDictionary *)dict

{

    NSString *jsonStr = @"数据异常,无法转成JSON";

  BOOL isValidJson =[NSJSONSerialization isValidJSONObject:dict];

  if (isValidJson) {

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict    options:NSJSONWritingPrettyPrinted error:nil];

  jsonStr = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];

}

return jsonStr;

}


(2) // 将JSON转成对象

+ (NSDictionary *)parseJsonFromStringOrObject:(NSString *)data

{

NSData *newData = [data dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

NSDictionary  * dict = [NSJSONSerialization JSONObjectWithData:newData options:NSJSONReadingAllowFragments error:nil];

return dict;

}


(3) 将任何对象转成json 字符串。(包括数组、字典,不包括自定义实体对象)

+ (NSString *)JSONString:(id)data

{

id result =  [NSJSONSerialization dataWithJSONObject:data options:NSJSONWritingPrettyPrinted error:nil];

NSString *jsonStr = [[NSString alloc]initWithData:result encoding:NSUTF8StringEncoding];

return jsonStr;

}

注: 的文本编辑器不太好用,太麻烦了,搞定代码格式有点难看!!希望官方能够有所改之。

你可能感兴趣的:(IOS APP 开发中遇到的问题总结)