iOS from Java

Comparisons are provided here as part of my experience in picking up iOS development. It should “aid” understanding, and is not meant to be taken to be equivalent exactly.

Concepts

  • Objective-C is the language used to code for the iOS, like Java.
  • Cocoa Touch is the framework that provides the UI components and classes, like Swing.
  • XCode is the IDE, like Eclipse, Netbeans (behaves more like Visual Studio).

Syntax

  • Classes are split into a “.h” interface and a “.m” implementation file.
  • Public methods are defined in “.h”, private in “.m” (Other classes can only ‘see’ methods defined in the .h file).
  • “#import” are like Java import statements.
  • “-” are like instance methods.
  • “+” are like static methods.
  • “@interface Z: A ” is like class Z extends class A, and implements B and C in Java.
  • B & C are known as protocols, like interfaces in Java.
  • “// /* */” are comments (same as Java)
  • “{ }” are code blocks, class blocks (same as Java)
  • “self” is like this in Java.
  • “[snake slither];” is like snake.slither(); in Java
  • “[snake yawn:3 andIntensity:10];” is like snake.yawn(3, 10); in Java. In objective-C the “andIntensity” is used by the caller, while inside the method body “intensity” refers to the variable.
  • @”abc” is a String literal, like “abc” in Java.

[Snake.java]

import example.Animal;

public class Snake extends Animal implements Predator, Prey {
  public static int getSomething() {
    return 1;
  }

  public void yawn(int seconds, int intensity) {
    // yawn
  }

  private void slither() {
    body.move("left");
  }
} 

[Snake.h]

#import "Animal.h"

@interface Snake: Animal 
+ (int)getSomething;
- (void)yawn:(int)seconds andIntensity:(int)intensity;
@end

[Snake.m]

@implementation Snake
+ (int)getSomething
{
  return 1;
}
- (void)yawn:(int)seconds andIntensity:(int)intensity
{
  // yawn
}
- (void) slither()
{
    [body move:@"left"];
}
@end

Types

  • BOOL is like a Java boolean (it can store more values but its intention is a boolean)
  • int is like a Java int
  • NSString is like a Java String
  • NSMutableArray is like a Java ArrayList (NSArray is not mutable)
  • NSMutableDictionary is like a Java HashMap (NSDictionary is not mutable)

Strings

TODO: stringWithFormat, stringByAppendingString, UTF8String, stringByAddingPercentEscapesUsingEncoding, substrings, length

Data Structures

TODO: Arrays and Dictionaries

Concurrency

TODO: NSOperation

Troubleshooting

lldb

  • Use “expr var” to print the value of a variable.
  • Use “po var” to print the contents of a pointer.

“EXC_BAD_ACCESS code=1”

The memory location you are accessing is invalid. It could mean:

The object is released more times than it is retained.
Check the stack trace of the offending thread just before the error. If there are multiple object invocations on that line, use lldb to print the retainCount for each of them. The offending object will give you the same error.

expr (int)[var retainCount]

There is no strong equivalent in Java, since it is using GC for reference counting. You might think of this as a NullPointerException, where the object has been set to null after the last release.

NSInvalidArgumentException: unrecognized selector sent to class

This is like a Java NoSuchMethodError. Invocations are only checked at runtime so this happens more frequently in Objective-C. First, read the error to see which call is causing the error. Then see which object is receiving this.

“EXC_BAD_ACCESS code=2”

The memory location you are accessing is invalid. It could mean:

The variable has not been initialized.
A reference could be declared but not yet initialized, e.g. a NSString* abc, and calling a.length before assigning a value to abc.

Stack overflow
I accidentally had a method calling itself instead of the method it overrides in the superclass, resulting in a infinite recursive call. The depth of the stack trace in the debug session tab should give you a clue about this.

Leave a Reply