How to Troubleshoot Apps for the Modern Connected Worker
Objective-C for Java Developers, Lesson 1
1. Objective-C for Java
Developers, Lesson 1
Raffi Khatchadourian
For Quinnipiac University
Monday, March 3, 2014
Inspired by:
Learn Objective-C for Java Developers by James Bacanek,
Apress 2009
4. Why is this topic
important?
Objective-C is the language typically
used to write native software for
Mac.
5. Why is this topic
important?
Objective-C is the language typically
used to write native software for
Mac.
No cross-
platform
interpretation
6. Why is this topic
important?
Objective-C is the language typically
used to write native software for
Mac.
No cross-
platform
interpretation
Mac UI look-
and-feel
7. Why is this topic
important?
Objective-C is the language typically
used to write native software for
Mac.
No cross-
platform
interpretation
Mac UI look-
and-feel
Non-native
languages?
8. Why is this topic
important?
Objective-C is the language
used to write
Mac.
Objective-C is the language used to
write native software for iOS.
9. Why is this topic
important?
Objective-C is the language
used to write
Mac.
Objective-C is the language used to
write native software for iOS.
Not
browser based
17. Objective-C
High-level, Object-Oriented language.
Roots in SmallTalk.
Strict superset of C with a lot of “twitter” (@
symbols).
Strongly-typed
Requires a compiler (llvm).
Language is multi-platform but not all libraries are
(Cocoa/Cocoa Touch).
23. Brief History
Began as a way to add Object-Oriented
Programming to C [Bacanek09].
Driven by Brad Cox and Tim Love in 1986.
Adopted by NeXT and used in the NEXTSTEP OS.
Faced fierce competition with C++.
Apple purchased NeXT in 1996, released Mac OS
X.
29. Header Files
// Student.h
Objective-C classes are split into two different files.
A header file (.h).
Contains the class declaration.
An implementation file (.m).
30. Header Files
// Student.h
Objective-C classes are split into two different files.
A header file (.h).
Contains the class declaration.
An implementation file (.m).
Contains the class definition.
31. Header Files
// Student.h
Objective-C classes are split into two different files.
A header file (.h).
Contains the class declaration.
An implementation file (.m).
Contains the class definition.
Clients (other programmers) receive the .h file and the
object file but not the .m file.
32. Header Files
// Student.h
Objective-C classes are split into two different files.
A header file (.h).
Contains the class declaration.
An implementation file (.m).
Contains the class definition.
Clients (other programmers) receive the .h file and the
object file but not the .m file.
Information
hiding,
abstraction
36. #import statements
// Student.h
#import <Foundation/Foundation.h>
#import is similar to Java import
statements.
Must by accessible to the compiler.
Processed by the pre-compiler.
37. #import statements
// Student.h
#import <Foundation/Foundation.h>
#import is similar to Java import
statements.
Must by accessible to the compiler.
Processed by the pre-compiler.
Must specify framework/library header file.
41. Class Declarations
// Student.h
#import <Foundation/Foundation.h>
@interface Student : NSObject
@end
@interface specifies the:
Class name.
Information about the class (e.g., subclass, implemented
protocols).
Similar to
java.lang.Object
42. Class Declarations
// Student.h
#import <Foundation/Foundation.h>
@interface Student : NSObject
@end
@interface specifies the:
Class name.
Information about the class (e.g., subclass, implemented
protocols).
No
multiple
inheritance
43. Class Declarations
// Student.h
#import <Foundation/Foundation.h>
@interface Student : NSObject
@end
@interface specifies the:
Class name.
Information about the class (e.g., subclass, implemented
protocols).
The interface of the class (instance variables, methods,
and properties)
44. Class Declarations
// Student.h
#import <Foundation/Foundation.h>
@interface Student : NSObject
@end
@interface specifies the:
Class name.
Information about the class (e.g., subclass, implemented
protocols).
The interface of the class (instance variables, methods,
and properties)
Delimited by @end.
47. Class Declarations
// Student.h
#import <Foundation/Foundation.h>
@interface Student : NSObject
@end
No packages (namespaces) in
Objective-C.
Use prefixes in class names (e.g.,
FBStudent)
No
packages
49. Class Properties
// Student.h
#import <Foundation/Foundation.h>
@interface Student : NSObject
@property (copy) NSString *firstName;
@end
A way for clients to indirectly access instance variables.
50. Class Properties
// Student.h
#import <Foundation/Foundation.h>
@interface Student : NSObject
@property (copy) NSString *firstName;
@end
A way for clients to indirectly access instance variables.
Can be read/write and read-only.
51. Class Properties
// Student.h
#import <Foundation/Foundation.h>
@interface Student : NSObject
@property (copy) NSString *firstName;
@end
A way for clients to indirectly access instance variables.
Can be read/write and read-only.
Compiler synthesizes accessor and mutator methods based on
property attributes.
52. Class Properties
// Student.h
#import <Foundation/Foundation.h>
@interface Student : NSObject
@property (copy) NSString *firstName;
@end
A way for clients to indirectly access instance variables.
Can be read/write and read-only.
Compiler synthesizes accessor and mutator methods based on
property attributes.
“Provide
clients with a copy of
firstName”
53. Class Properties
// Student.h
#import <Foundation/Foundation.h>
@interface Student : NSObject
@property (copy) NSString *firstName;
@end
A way for clients to indirectly access instance variables.
Can be read/write and read-only.
Compiler synthesizes accessor and mutator methods based on
property attributes.
“Provide
clients with a copy of
firstName”
Could
also use strong or
weak
54. Class Properties
// Student.h
#import <Foundation/Foundation.h>
@interface Student : NSObject
@property (copy) NSString *firstName;
@end
A way for clients to indirectly access instance variables.
Can be read/write and read-only.
Compiler synthesizes accessor and mutator methods based on
property attributes.
You can create your own in the implementation file.
“Provide
clients with a copy of
firstName”
Could
also use strong or
weak
55. Class Properties
// Student.h
#import <Foundation/Foundation.h>
@interface Student : NSObject
@property (copy) NSString *firstName;
@end
A way for clients to indirectly access instance variables.
Can be read/write and read-only.
Compiler synthesizes accessor and mutator methods based on
property attributes.
You can create your own in the implementation file.
NSString * is a reference to a string object (String firstName).
“Provide
clients with a copy of
firstName”
Could
also use strong or
weak
63. Objective-C Memory
Management
Two types of references:
Weak:
“I need to refer to this object but I don’t
own it.”
Strong:
“I am an owner of this object.”
Objects are deallocated when no strong references
to them remain.
102. Student Class Definition
// Student.m
#import "Student.h"
@implementation Student
- (NSString *)fullName {
return [NSString stringWithFormat:@"%@ %@",
[self firstName],
[self lastName]];
}
@end
Method invocation between []
Format string is similar to C and Java OutputStream.format()
103. Student Class Definition
// Student.m
#import "Student.h"
@implementation Student
- (NSString *)fullName {
return [NSString stringWithFormat:@"%@ %@",
[self firstName],
[self lastName]];
}
@end
Method invocation between []
Format string is similar to C and Java OutputStream.format()
@ means object reference
104. Student Class Definition
// Student.m
#import "Student.h"
@implementation Student
- (NSString *)fullName {
return [NSString stringWithFormat:@"%@ %@",
[self firstName],
[self lastName]];
}
@end
Method invocation between []
Format string is similar to C and Java OutputStream.format()
@ means object reference
Class
method
105. Student Class Definition
// Student.m
#import "Student.h"
@implementation Student
- (NSString *)fullName {
return [NSString stringWithFormat:@"%@ %@",
[self firstName],
[self lastName]];
}
@end
Method invocation between []
Format string is similar to C and Java OutputStream.format()
@ means object reference
Argument list
106. Student Class Definition
// Student.m
#import "Student.h"
@implementation Student
- (NSString *)fullName {
return [NSString stringWithFormat:@"%@ %@",
[self firstName],
[self lastName]];
}
@end
Method invocation between []
Format string is similar to C and Java OutputStream.format()
@ means object reference
Method
name
107. Student Class Definition
// Student.m
#import "Student.h"
@implementation Student
- (NSString *)fullName {
return [NSString stringWithFormat:@"%@ %@",
[self firstName],
[self lastName]];
}
@end
Method invocation between []
Format string is similar to C and Java OutputStream.format()
@ means object reference
Like Java
this
108. Student Class Definition
// Student.m
#import "Student.h"
@implementation Student
- (NSString *)fullName {
return [NSString stringWithFormat:@"%@ %@",
[self firstName],
[self lastName]];
}
@end
Method invocation between []
Format string is similar to C and Java OutputStream.format()
@ means object reference
Property
109. Student Class Definition
// Student.m
#import "Student.h"
@implementation Student
- (NSString *)fullName {
return [NSString stringWithFormat:@"%@ %@",
[self firstName],
[self lastName]];
}
@end
Method invocation between []
Format string is similar to C and Java OutputStream.format()
@ means object reference
111. Conclusion
Both Objective-C and Java are high-level, compiled,
Object-Oriented languages.
Objective-C is not cross-platform but can be faster
than Java.
Java is very structured, while Objective-C can be
either structured or dynamic.
Objective-C is a strict superset of C.
Objective-C uses automatic reference counting
(ARC) instead of garbage collection.
Thanks for coming to my talk. My name is Raffi Khatchadourian and today I’ll be giving a brief, very first lesson in introducing the Objective-C language and its differences with Java. This talk is inspired by the book entitled Objective-C for Java Developers by James Bacanek, published by Apress in 2009. Please feel free to stop me to ask any questions along the way.
This talk is focused on an audience familiar with Object-Oriented Programming concepts particularly in Java. But, you should be able to relate to the topics discussed if you’re familiar with any Object-Oriented language.
Why did I chose this topic and why I think it is important? Well, Objective-C happens to be the language typically used to write native software for Mac, which is a platform gaining some popularity. According to Gartner, there was a 28.5% increase in home mac sales just last year.
What do you think I mean by native here? Any ideas?
Native means that machine instructions produced by the compiler are directly executed by the machine, as opposed to non-native software, where instructions are executed by a virtual machine or interpreter.
Native software is specifically made for a particular platform, so there’s no cross-platform capabilities (i.e., compile once, run anywhere), and you get a native look-and-feel for your software. Can anyone think of any languages that are non-native, cross-platform?
Of course, iOS has become extremely popular with mobile users. iOS is much more of a closed system. Non-native iOS software would be browser based (what language?).
No UI stuff.
Both Java and Objective-C are high-level, object-oriented languages. What do I mean by high level? Object-oriented?
Objective-C is based on SmallTalk, an early Object-Oriented language that conceptualized entities (objects) sending and receiving messages to each other to collaboratively solve a task.
Like Java, Objective-C can be strongly-typed in that developers can specify types of variables and have certain expectations of the type of variables at run time. Unlike Java, however, Objective-C doesn’t force the developer to always use types, and there’s lots of language support to dynamically alter the behavior at runtime. This can be done in Java as well but much more cumbersomely with the reflection API.
With Objective-C, you can defer many decisions to when the program is executed, which allows for great flexibility. For example, you can add a method to the standard string class without subclassing. That means you can call your method anywhere a string is used. Has flavors of AOP.
Both Java and Objective-C need a compiler. llvm is a popular Objective-C compiler. (Q: What language doesn’t need a compiler?)
Cocoa is the name of the framework that contains many of the classes used to build mac and iOS applications. Cocoa is similar to the JDK.
It is a direct extension of C, which is the fastest high-level language available. Does anyone know how C can be used in Java? Yes! It can be done using the Java Native Interface (JNI), but since Objective-C is in a sense C, no overhead is associated with accessing the native machine.
Can be similar to a scripting language if you like. Many decisions left to run time. Can “attach” methods to existing class hierarchies.
There are some massive differences in syntax and conventions in Objective-C, particularly in method invocation. We’ll discuss those later.
There is no garbage collection in Objective-C. There is a facility called “ARC,” which stands for Automated Reference Counting. It is similar to garbage collection but with some key differences. Also, there’s a notion of objects owning other objects, a relationship that is specified by the developer. This tells the ARC system when to deallocate objects.
You’ll see a lot of NS’s around.
What else happened in 1996?
In Java, a class is defined in a single file. In Objective-C (like C++), classes are split between two different files. This is somewhat similar to Java interfaces where you could have an interface file for each class. However, there is another notion in Objective-C similar to a Java interface called a protocol.
In most cases, there’s a one-to-one relationship between header files and implementation files, and they normally have the same name except for the extension. This is to enable information hiding.
There are cases where this isn’t necessarily true. Can you think of a situation where one header file may have multiple definitions? (ANS: platform specific code)
#import is very similar to Java import statements. Here, we’re telling the compiler that we’ll be using classes declared in the header file Foundation.h in the Foundation framework. This is the main Cocoa framework umbrella header file (it has a bunch of #import statements in it itself).
Unlike Java, the compiler will not use a class path to find the file. You need to tell the compiler where your classes are, either through the Xcode project file (a configuration file telling the compiler how to build your product, similar to ant) or the DYLD_LIBRARY_PATH environmental variable.
@interface specifies the interface of the class, but it should not be confused with the Java interface.
NSObject is similar to java.lang.object as it is the root of all Cocoa class hierarchies.
Inheritance works very similar to Java.
One thing to note is that Objective-C doesn’t have the notion of packages that Java has.
Is anyone familiar with C++? What is the equivalent there? ANS: Namespace
C has no namespaces or packages and neither does Objective-C.
Prefix your type names. For example, we were working at facebook, maybe we’d call this class FBStudent.
Common Cocoa prefix is NS. Anyone guess why?
You can set Xcode to automatically do this for you.
Synthesize is another word for automatic generation. The compiler will create accessor and mutator methods for you based on these property attributes.
Why would we want to provide clients with a copy of the string firstName?
ANS: Strings are immutable in Java but that’s not always the case in Objective-C. In fact, there are mutable subclasses of many immutable types in Objective-C. That’s a difference between Objective-C and Java.
strong and weak are object ownership property attribute. They specify strong and weak references to other objects. This controls how Objective-C manages memory deallocation.
Let’s break here from the Student class to discuss how Objective-C manages and cleans up memory allocated on the heap. The Objective-C runtime no longer includes a garbage collector (some history here). Instead, it has ARC (anyone know this? ANS: automated reference counting).
Let’s break here from the Student class to discuss how Objective-C manages and cleans up memory allocated on the heap. The Objective-C runtime no longer includes a garbage collector (some history here). Instead, it has ARC (anyone know this? ANS: automated reference counting).
There are some consequences here.
Funny that both these photos are taken in St. Louis!
What happens?
Trivial?
Q: Are there tools to detect this? Can we determine this at compile time?
Let’s get back to properties. Now, we’ll add a “full name” derived property.
Non-atomic for simplicity. Why would you want this atomic?
Could we have written fullName as a normal method? You get a feel for when to write a property and when to write a method.
What is the symbol for a class method?
Why should we use properties within classes? ANS: Derived class.