iOS: Why can’t I set nil to NSDictionary value?
It wants an actual object… use NSNull [NSNull null];
It wants an actual object… use NSNull [NSNull null];
SEL is just a pointer, which you could store in an NSValue: NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: [NSValue valueWithPointer:@selector(foo)], @”foo”, nil]; To get the selector back, you can use: SEL aSel = [[dict objectForKey:@”foo”] pointerValue];
You can use NSKeyedArchiver and NSKeyedUnarchiver Example for swift 2.0+ var dictionaryExample : [String:AnyObject] = [“user”:”UserName”, “pass”:”password”, “token”:”0123456789″, “image”:0] let dataExample : NSData = NSKeyedArchiver.archivedDataWithRootObject(dictionaryExample) let dictionary:NSDictionary? = NSKeyedUnarchiver.unarchiveObjectWithData(dataExample)! as? NSDictionary Swift3.0 let dataExample: Data = NSKeyedArchiver.archivedData(withRootObject: dictionaryExample) let dictionary: Dictionary? = NSKeyedUnarchiver.unarchiveObject(with: dataExample) as! [String : Any] Screenshot of playground
In the ‘File Attribute Keys’ of the NSFileManager class reference you can see that there is no key to use that will return the duration of a song. All the information that the NSFileManager instance gets about a file is to do with the properties of the actual file itself within the operating system, such … Read more
You can call [aDictionary description], or anywhere you would need a format string, just use %@ to stand in for the dictionary: [NSString stringWithFormat:@”my dictionary is %@”, aDictionary]; or NSLog(@”My dictionary is %@”, aDictionary);
I think this will do it: brandDescriptor = [[NSSortDescriptor alloc] initWithKey:@”brand” ascending:YES]; sortDescriptors = [NSArray arrayWithObject:brandDescriptor]; sortedArray = [myArray sortedArrayUsingDescriptors:sortDescriptors]; I pulled the code from Sort Descriptor Programming Topics. Also, Key-Value Coding comes into play, in that sortedArrayUsingDescriptors: will send a valueForKey: to each element in myArray, and then use standard comparators to sort the … Read more
The NSDictionary and NSMutableDictionary docs are probably your best bet. They even have some great examples on how to do various things, like… …create an NSDictionary NSArray *keys = [NSArray arrayWithObjects:@”key1″, @”key2″, nil]; NSArray *objects = [NSArray arrayWithObjects:@”value1″, @”value2″, nil]; NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; …iterate over it for (id key in dictionary) { … Read more
Use NSSortDescriptor like this.. NSSortDescriptor * descriptor = [[NSSortDescriptor alloc] initWithKey:@”interest” ascending:YES]; stories = [stories sortedArrayUsingDescriptors:@[descriptor]]; recent = [stories copy]; stories is the array you want to sort. recent is another mutable array which has sorted dictionary values. Change the @”interest” with the key value on which you have to sort. All the best
I presume that [dataArray objectAtIndex:indexPathSet.row] is returning an NSDictionary, in which case you can simply check the result of valueForKey against nil. For example: if ([[dataArray objectAtIndex:indexPathSet.row] valueForKey:@”SetEntries”] != nil) { // The key existed… } else { // No joy… }
-[NSDictionary allKeysForObject:] returns an NSArray of all the keys whose objects match the passed object, where “match” is determined by isEqual:.