Tuesday, April 1, 2014

Create an array


1
  NSArray *array = [NSArray arrayWithObjects:@"One", [NSNumber numberWithInt:1], nil];

Or use literal array expression:-

1
  NSArray *array =@[ @"One", [NSNumber numberWithInt:1] ];

Use NSNumber to add boolean or integer values to an array, or prefix a boolean value with ‘@':-

1
  NSArray *array = @[ [NSNumber numberWithInt:1], [NSNumber numberWithBool:YES], @NO ];

Otherwise we will get an error like this:-

     Collection element of type ‘NSInteger’ (aka ‘int’) is not an Objective-C object

Use NSNull to add a nil equivalent value to an array:-

1
  NSArray *array = @[ @"a", [NSNull null], @"b" ];


Refs
http://stackoverflow.com/questions/1602572/how-to-create-an-array-of-strings-in-objective-c-for-iphone
http://clang.llvm.org/docs/ObjectiveCLiterals.html
http://stackoverflow.com/questions/1309535/why-does-nsarray-arraywithobjects-require-a-terminating-nil
http://stackoverflow.com/questions/2057910/how-to-add-nil-to-nsmutablearray

Saturday, March 29, 2014

Managed Object ID


  • Get the string URL representation from a ManagedObjectID
1
2
  NSURL *urlID = [objectID URIRepresentation];
  NSString *strUrlID = [urlID absoluteString];

  • Get the ManagedObjectID from a URL string
1
2
  NSURL *urlID = [[NSURL alloc] initWithString:strUrlID];
  NSManagedObjectID *objectID = [[managedObjectContext persistentStoreCoordinator] managedObjectIDForURIRepresentation:urlID];

  • Get the ManagedObject by a ManagedObjectID
1
2
  NSError *error = nil;
  NSManagedObject *managedObject = [moc existingObjectWithID:objectID error:&error];

  • Check if a ManagedObject is new
1
  BOOL isNew = managedObject.objectID.isTemporaryID;

  • Get the permanent object IDs for the objects in a child context.
1
2
3
4
5
6
7
8
  NSError *error = nil;
  [childManagedObjectContext obtainPermanentIDsForObjects:childManagedObjectContext.insertedObjects.allObjects error:&error]);

  // . . . Check for an error . . .

  [childManagedObjectContext save:&error];

  // . . . Check for an error . . .


Refs
http://stackoverflow.com/questions/3519334/how-to-uniquely-identify-nsmangedobject-with-string
http://stackoverflow.com/questions/10811280/how-to-get-the-objectid-from-url
http://stackoverflow.com/questions/3013288/how-to-use-managedobjectid-the-right-way
http://iphonedevelopment.blogspot.com.au/2009/07/core-data-determining-if-managed-object.html
http://stackoverflow.com/questions/11990279/core-data-do-child-contexts-ever-get-permanent-objectids-for-newly-inserted-obj/11996957#11996957

Thursday, March 27, 2014

Compare NSDate instances


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
  NSComparisonResult result = [date1 compare:date2];  
  if (result == NSOrderedAscending)
  {
    // date1 < date2
  }
  else if (result == NSOrderedDescending)
  {
    // date1 > date2
  }
  else
  {
    // date1 == date2
  }

or

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
  if ([date1 timeIntervalSinceReferenceDate] < [date2 timeIntervalSinceReferenceDate])
  {
    // date1 < date2
  }
  else if ([date1 timeIntervalSinceReferenceDate] > [date2 timeIntervalSinceReferenceDate])
  {
    // date1 > date2
  }
  else
  {
    // date1 == date2
  }

Refs
http://stackoverflow.com/questions/5965044/how-to-compare-two-nsdates-which-is-more-recent

lldb Error - no known method '-class'; cast the message send to the method's return type

p [[navigationController topViewController] class] 

returns

error: no known method '-class'; cast the message send to the method's return type 

To fix it, cast it first:- 

p [(NSObject *) [navigationController topViewController] class]

Refs
http://stackoverflow.com/questions/14007942/lldb-error-property-not-found-on-object-of-type