Monday, February 1, 2016

Have fun with cross platform mobile development

A short presentation about a sample cross platform mobile project called TumChatter for both Android and iOS.





You can find the project on github at the link below:-
https://github.com/have-fun/TumChatter

Programming Language: Kotlin
Frameworks: Java, RoboVM
Dependency Injection: Dagger 2
Json: Gson
REST Client: Retrofit
Reactive Libraries: RxKotlin, RxJava
Unit Testing: Spek (Specification Framework), AssertJ, Mockito
Embedded Server: TumWeb, Nanohttpd
Android UI Binding: KotterKnife, ButterKnife


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

Monday, November 25, 2013

Start/Stop PostgreSQL on Mac

Start PostgreSQL:-
sudo -u postgres /Library/PostgreSQL/9.3/bin/pg_ctl -D /Library/PostgreSQL/9.3/data start
Stop PostgreSQL:-
sudo -u postgres /Library/PostgreSQL/9.3/bin/pg_ctl -D /Library/PostgreSQL/9.3/data stop
Stop PostgreSQL from auto-starting on reboot:-
sudo launchctl unload -w /Library/LaunchDaemons/com.edb.launchd.postgresql-9.3.plist 
Make PostgreSQL auto-start on reboot:-
sudo launchctl load -w /Library/LaunchDaemons/com.edb.launchd.postgresql-9.3.plist 
Refs
http://www.xtuple.org/faq/xtuple-faq/postgresql/how-do-i-stop-postgresql-service-mac 
https://gist.github.com/abhishekdev/1960888

Saturday, August 17, 2013

Run Tomboy with Mono 3.2 on Mac

Compiling it from scratch based on http://xmemory.tompium.com/2012/06/running-tomboy-note-taking-app-on-mac.html doesn't work anymore. Here are the additional instructions:-
  • After getting the latest version of Tomboy (1.15.3),  replace "flush(true)" with "flush()" in Note.cs to fix a compile error. Not sure why the compiler complained about this line. Anyway, using just "flush()" fixes the problem but it may not write everything to disk straight away.
  • After building Tomboy, running it caused an exception:- 
Missing method System.Reflection.Assembly::op_Equality(Assembly,Assembly)
To fix it, modify a script file called Tomboy in Tomboy.app -> Contents/MacOS/Tomboy by changing the last line to
exec -a "Tomboy" "$MONO" --runtime=v4.0 --debug $ASSEMBLY $@
Refs
https://bugs.launchpad.net/ubuntu/+source/mono/+bug/882501

http://xmemory.tompium.com/2012/06/running-tomboy-note-taking-app-on-mac.html