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

Syntax

[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 <Predator, Prey>
+ (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

Strings

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

Data Structures

TODO: Arrays and Dictionaries

Concurrency

TODO: NSOperation

Troubleshooting

lldb

“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.

RSS feed for comments on this post · TrackBack URL

Leave a Comment