I’m asking this question as someone from a PHP background. In php, arrays are insanely simple to create: $numberArray = array(‘minute’=>$minute, ‘hour’=$hour) etc.
As I’m getting into Obj-c / Cocoa, I’ve noticed that NSArray only stores flat arrays,
NSMuteableArray *numberArray = [[NSMuteableArray init] alloc];
[numberArray insertObject:minute atIndex:0];
// not sure if above is correct, but it's a moot point
// Creates a non-associate array (numberArray = '1', '2', '3', etc.)
NSDictionary allows you to create an associate arrays, but the values must be objects.
NSMuteableDictionary *numberArray = [[NSMuteableDictionary alloc] init];
[numberArray setObject:minute forKey:@"minutes"];
// numberArray creates array of ('minute'=>minuteObject, 'hour'=>hourObject, etc.)
Here’s my question: what array or dictionary format allows you to create an array where the VALUE is something Other than an object? I’m trying to create a method that grabs the current time’s minutes / hours and then inserts them into an array. From that method, when I need it elsewhere in my application I can call this method and get the appropriate value from the key. Here’s my full code:
NSDate *now = [NSDate date];
NSCalendar *gregorianCal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComps = [gregorianCal components: (NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate: now];
// Then use it
NSUInteger minute = [dateComps minute];
NSUInteger hour = [dateComps hour];
NSMutableDictionary *timeArray = [[NSMutableDictionary alloc] init];
[timeArray setValue:minute forKey:@"minute"];
Please correct me if I’m wrong in any of my assumptions. As someone who is new to app dev, my prior statements were more for “this is what I’ve learned so far, please correct me if I’m wrong” rather than “I’m right, you’re wrong”.
With the Cocoa framework, in common with many object-oriented frameworks/languages (and similar to what PHP is doing under the hood), you can only store objects in collections. To store primitive value types you wrap them as objects. For numbers the class is
NSNumber.Change your last line to:
and it should work. To get the
NSUIntegerback out you need to unwrap theNSNumber, something along the lines of: