Tag: nsdictionary

如何检查值null并将其replace在NSDictionary中

现在,我正在使用NSUserDefault和NSDictionary,我保存在NSUserDefault的NSDictionary,不幸的是我不能,因为NSDictionary返回到Json有空值。 我需要检查,如果NSDictionary具有空值和replace。 怎么样? 这是NSDictionary, ({ ID:11, name:21-12-2012, des:"<null>", url: [ { ID:1, name: "<null>" }, { ID:2, name:"<null>" } ] }, { ID:12, name:if i die young, des:"<null>", url: [ { ID:3, name: "<null>" }, { ID:21, name:"<null>" } ] })

从字典中删除嵌套的密钥

假设我有一个相当复杂的词典,就像这样: let dict: [String: Any] = [ "countries": [ "japan": [ "capital": [ "name": "tokyo", "lat": "35.6895", "lon": "139.6917" ], "language": "japanese" ] ], "airports": [ "germany": ["FRA", "MUC", "HAM", "TXL"] ] ] 我可以访问所有的领域, if let ..块可选地铸造的东西,我可以使用,在阅读时。 不过,我目前正在编写unit testing,我需要以多种方式select性地打破字典。 但我不知道如何优雅地从字典中删除键。 例如,我想在一个testing中删除关键"japan" ,在下一个"lat"应该是零。 这是我目前的实现去除"lat" : if var countries = dict["countries"] as? [String: Any], var japan = […]

NSDictionary获取按字母顺序sorting

我正在通过NSDictionary我的函数作为参数。 我希望这是关键和价值观,因为我插入。 例如。 预期产出是: mutable dict:{ zKey1 = Value1; fKey2 = Value2; aKey3 = Value3; } 我曾尝试以下方法来创build和设置键的值。 NSMutableDictionary *mutableDict = [[NSMutableDictionary alloc]init]; [mutableDict setObject:@"Value1" forKey:@"zKey1"]; [mutableDict setObject:@"Value2" forKey:@"fKey2"]; [mutableDict setObject:@"Value3" forKey:@"aKey3"]; NSMutableDictionary *dic2=[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"1004",@"userId",@"cucumber",@"domain",@"168d5c02f ",@"token",@"1004",@"userId1",@"cucumber",@"domain1",@"168d5c02f ",@"token1", nil]; NSDictionary * dict = [NSDictionary dictionaryWithObjects:@[@"Ravi",@"33",@"India",@"India"] forKeys:@[@"name",@"age",@"location",@"country"]]; NSArray *sortedKeys = [[dict allKeys] sortedArrayUsingSelector: @selector(compare:)]; NSMutableArray *sortedValues = [NSMutableArray […]

观察NSArray中NSDictionary的值更改

这是我的NSArray carType ( { isSelected = 0; kFieldName = "All types"; kObjectValue = 0; kObjectValueText = All; }, { isSelected = 0; kFieldName = "4 seats"; kObjectValue = 4; kObjectValueText = "4 seats"; }, { isSelected = 1; kFieldName = "7 seats"; kObjectValue = 7; kObjectValueText = "7 seats"; } ) 我如何观察isSelected字段的变化? 我在我的代码中使用了KVO,但它不起作用。 carType是context的NSArray属性 – […]

NSLocalizedString作为NSDictionary的关键

我在这个论坛上读到,可以使用NSLocalizedString作为NSDictionary的关键。 这是我的NSDictionary: LABELS = [[NSDictionary alloc] initWithObjectsAndKeys: NSLocalizedString(@"Threshold 0", @"Description for threshold 0") , @"threshold_0", NSLocalizedString(@"Threshold 1", @"Description for threshold 1"), @"threshold_1", NSLocalizedString(@"Threshold 2", @"Description for threshold 2"), @"threshold_2", NSLocalizedString(@"Threshold 3", @"Description for threshold 3"), @"threshold_3", NSLocalizedString(@"Threshold 4", @"Description for threshold 4"), @"threshold_4", nil]; 这是试图访问NSDictionary的代码: NSString *key = [NSString stringWithFormat: @"threshold_%d",{MY_VARIABLE}]; NSString *text = [LABELS […]

JSON格式:以正确的顺序获取输出

常见任务,将string转换为JSON格式。 是的,容易,StackOverflow关于这个的很多答案。 不容易的是,我明白为什么我得到的顺序不同于我把对象放在NSDictionary中的顺序。 这是我写的代码: -(NSString*)createJSONstring { //http://www.raywenderlich.com/5492/working-with-json-in-ios-5 NSDictionary* dictionary = [NSDictionary dictionaryWithObjectsAndKeys: @"<xx3-xxxx>",@"from", @"<xxx-xxx>",@"to", @"<Bla bla bla>",@"message", @"<2000>",@"posixtime", nil]; NSArray* notifications = [NSArray arrayWithObjects:dictionary, nil]; NSError *writeError = nil; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:notifications options:NSJSONWritingPrettyPrinted error:&writeError]; NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; NSLog(@"JSON Output: %@", jsonString); return jsonString; } 我希望得到这样的东西: { "from" : "<xx3-xxxx>" […]

sortingNSDates

我有我的程序中的几个字典,我已经把所有这些字典在一个数组我想sorting这些date,因此字典应交换其位置,下面显示的是我使用的代码,但我没有得到它sorting。请帮助我解决这个问题 ** NSLog(@“sort —%d”,[allDataArray count]); for(int i=0;i<[allDataArray count]-1;i++){ NSString *dateStr1=[[allDataArray objectAtIndex:i]objectForKey:@"Date"]; for(int j=i+1;j<[allDataArray count];j++){ NSString *dateStr2=[[allDataArray objectAtIndex:j]objectForKey:@"Date"]; if(([(NSDate*)dateStr1 compare:(NSDate*)dateStr2]==NSOrderedAscending) ||([(NSDate*)dateStr1 compare:(NSDate*)dateStr2]==NSOrderedSame)) [allDataArray replaceObjectAtIndex:i withObject:[allDataArray objectAtIndex:j]]; } } NSLog(@"afterSorting-numbers–%@",allDataArray);**

使用比较器对NSDictionaries的NSArray进行sorting

我一直在尝试使用比较器对NSDictionaries的NSArray进行sorting,但我似乎无法得到我想要的输出。 我试图达到的输出是,AZ的用户名应该排在第一位的sorting数组,然后以数字开头的用户名应该排在第二位的sorting数组,最后用户名以下划线开始应该在sorting的数组中最后。 任何帮助真正感激! 编辑:它应该被sorting,所以它看起来整个NSArray一致,以便:_Anna之前_Bob和_11Bob之前_12Cary但之后_09Bob 我期待的输出示例: ( { username = abcd; }, { username = Anna; }, { username = 01Bob; }, { username = 02Tob; }, { username = 03ZED; }, { username = 04_Hob; }, { username = 04_sob; }, { username = "_anna"; }, { username = "_bob"; }, { username = "_boc"; […]

NSUserDefaults无法保存NSMutableDictionary

Iam试图用这个代码保存NSMutableDictionary到NSUserDefaults: NSUserDefaults *def = [NSUserDefaults standardUserDefaults]; NSMutableDictionary *repositoryData = [[NSMutableDictionary alloc] init]; [repositoryData setObject:personData forKey:@"persondata"]; [def setObject:repositoryData forKey:@"kUserRepository"]; [def synchronize]; [repositoryData removeAllObjects]; [repositoryData release]; 但是我得到这个味精: [NSUserDefaults setObject:forKey:]: Attempt to insert non-property value '{ persondata = "<Person: 0x1ed57e70>"; }' of class '__NSDictionaryM'. Note that dictionaries and arrays in property lists must also contain only property values. […]

如何有效地设置HTTP身体请求?

在我的应用程序中,我目前正在从每个viewcontroller发送http请求。 但是,目前我正在实现一个类,这应该有发送请求的方法。 我的要求在参数数量上有所不同。 例如,要得到tableview的东西的列表,我需要把类别,子类别,filter和5个以上的参数请求。 这就是我的请求现在看起来像: NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init]; [request setValue:verifString forHTTPHeaderField:@"Authorization"]; [request setURL:[NSURL URLWithString:@"http://myweb.com/api/things/list"]]; [request setHTTPMethod:@"POST"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; NSMutableString *bodyparams = [NSMutableString stringWithFormat:@"sort=popularity"]; [bodyparams appendFormat:@"&filter=%@",active]; [bodyparams appendFormat:@"&category=%@",useful]; NSData *myRequestData = [NSData dataWithBytes:[bodyparams UTF8String] length:[bodyparams length]]; [request setHTTPBody:myRequestData] 我的第一个想法是创build方法,接受所有这些参数,那些不需要的将是零,那么我会testing哪些是零,那些不是零将附加到参数string(毫秒)。 然而这是相当低效的。 后来我正在考虑传递一些带有存储值的字典作为参数。 像在Android的Java中使用nameValuePair的数组列表。 我不知道,我将如何获得我的字典中的键和对象 -(NSDictionary *)sendRequest:(NSString *)funcName paramList:(NSDictionary *)params { // now I need […]