View
14
Download
0
Category
Preview:
Citation preview
Week 1 PresentationIntroduction to Ubiquitous Computing
COSC 4355/6355Fall 2015
Ioannis PavlidisAshik Khatri
Dinesh Majeti
Computational Physiology Lab
ipavlidis@uh.eduarkhatri@uh.edudmajeti@uh.edu
September 3, 2015
(CPL) Week 1 Presentation September 3, 2015 1 / 24
Overview
1 Objective-CPropertiesFunctionsClassesObjectsCollections
2 XcodeIBOutletsIBAction
3 References
(CPL) Week 1 Presentation September 3, 2015 2 / 24
Evaluation
(CPL) Week 1 Presentation September 3, 2015 3 / 24
Evaluation
(CPL) Week 1 Presentation September 3, 2015 4 / 24
Objective-C
Objective-C separates the interface declaration and theimplementation of a class in .h and .m files.
All functionality that shall be available to other classes needs to beplaced in the .h file of a class, all implementations and all privatefunctionality is placed in the .m file.
(CPL) Week 1 Presentation September 3, 2015 5 / 24
Properties
The goal of the @property directive is to make it easy to create andconfigure properties by automatically generating accessor methods.
Property attributes indicate data accessibility and storageconsiderations.
(CPL) Week 1 Presentation September 3, 2015 6 / 24
Properties
readonly - Don’t synthesize a setter method.
nonatomic - Don’t guarantee the integrity of accessors in amulti-threaded environment. This is more efficient than the defaultatomic behavior.
strong - Create an owning relationship between the property and theassigned value. This is the default for object properties.
@property (nonatomic, strong) Car *car;
weak - Create a non-owning relationship between the property andthe assigned value. Use this to prevent retain cycles.
copy - Create a copy of the assigned value instead of referencing theexisting instance.
(CPL) Week 1 Presentation September 3, 2015 7 / 24
Functions
Example C function -
void setName(String name);
void setFirstNameAndLastName(String firstName,
String lastName);
Functions in Objective-C
- (void)setName:(NSString*)name;
- (void)setFirstName:(NSString*)firstName
andLastName:(NSString*)lastName;
(CPL) Week 1 Presentation September 3, 2015 8 / 24
Classes
The Objective-C syntax used to declare a class interface looks likethis:
@interface SimpleClass : NSObject
@end
This example declares a class named SimpleClass, which inheritsfrom NSObject.
Class methods are denoted by the use of a + sign, whichdifferentiates them from instance methods using a − sign.+ (id)stringWithString:(NSString *)aString;
(CPL) Week 1 Presentation September 3, 2015 9 / 24
Objects
Objects send and receive messages.
[someObject doSomething];
When the above line of code is executed, someObject will be sentthe doSomething message.
Objects can send messages to themselves.
@implementation XYZPerson
- (void)sayHello {
[self saySomething:@"Hello, world!"];
}
- (void)saySomething:(NSString *)greeting {
NSLog(@"%@", greeting);
}
@end
(CPL) Week 1 Presentation September 3, 2015 10 / 24
Objects
Objects are created dynamically.
The NSObject root class provides a class method, alloc, whichhandles this process for you
+ (id)alloc;
The alloc method has one other important task, which is to clearout the memory allocated for the object’s properties by setting themto zero. This avoids the usual problem of memory containing garbagefrom whatever was stored before, but is not enough to initialize anobject completely.
- (id)init;
NSObject *newObject = [[NSObject alloc] init];
(CPL) Week 1 Presentation September 3, 2015 11 / 24
String Objects
Strings are represented by NSString class.
The basic NSString class is immutable.
NSString *name = @"John";
name = [name stringByAppendingString:@"ny"];
// returns a new string object
The NSMutableString class is the mutable subclass of NSString,and allows you to change its character contents at runtime usingmethods like appendString: or appendFormat:, like this:
NSMutableString *name = [NSMutableString
stringWithString:@"John"];
[name appendString:@"ny"]; // same object,
but now represents "Johnny"
(CPL) Week 1 Presentation September 3, 2015 12 / 24
Collections
Most collections in Objective-C code are instances of one of theCocoa and Cocoa Touch collection classes, like NSArray, NSSet
and NSDictionary.
These classes are used to manage groups of objects and areimmutable.
(CPL) Week 1 Presentation September 3, 2015 13 / 24
Array
An NSArray is used to represent an ordered collection of objects. Theonly requirement is that each item is an Objective-C object - there’sno requirement for each object to be an instance of the same class.
NSArray *someArray =
[NSArray arrayWithObjects:someObject,
someString, someNumber, someValue, nil];
NSArray *someArray = @[firstObject, secondObject,
thirdObject];
(CPL) Week 1 Presentation September 3, 2015 14 / 24
Dictionary
NSDictionary stores objects against given keys, which can then beused for retrieval.
NSDictionary *dictionary =
[NSDictionary dictionaryWithObjectsAndKeys:
someObject, @"anObject",
@"Hello, World!", @"helloString",
@42, @"magicNumber",
someValue, @"aValue",
nil];
Each object is specified before its key, and the list of objects and keysmust be nil-terminated.
(CPL) Week 1 Presentation September 3, 2015 15 / 24
”No object”
It’s not possible to add nil to the collection classes described in thissection because nil in Objective-C means ”no object.”
If you need to represent ”no object” in a collection, you can use theNSNull class:
NSArray *array = @[ @"string", @42, [NSNull null] ];
NSNull is a singleton class, which means that the null method willalways return the same instance.
(CPL) Week 1 Presentation September 3, 2015 16 / 24
Xcode
Figure 1: Xcode interface
(CPL) Week 1 Presentation September 3, 2015 17 / 24
Xcode
Figure 2: Xcode toolbar
(CPL) Week 1 Presentation September 3, 2015 18 / 24
Xcode
Figure 3: Xcode utilities area
(CPL) Week 1 Presentation September 3, 2015 19 / 24
Outlets
An outlet is a property of an object that references another object.
The containing object holds an outlet declared as a property with thetype qualifier of IBOutlet and a weak option.
An outlet is declared as a weak reference to prevent strong referencecycles.@property (weak) IBOutlet NSArray *keywords;
(CPL) Week 1 Presentation September 3, 2015 20 / 24
Actions
Whenever the user activates a control, such as by tapping it, thecontrol should send a message instructing your code to perform someaction. You can do this by creating an IBAction.
(CPL) Week 1 Presentation September 3, 2015 21 / 24
Actions
(CPL) Week 1 Presentation September 3, 2015 22 / 24
References
Objective-C Properties -https://developer.apple.com/library/ios/documentation/
Cocoa/Conceptual/ProgrammingWithObjectiveC/
EncapsulatingData/EncapsulatingData.html
(CPL) Week 1 Presentation September 3, 2015 23 / 24
On the lighter side...
I once told an Objective-C joke, but nobody got the message! :)
(CPL) Week 1 Presentation September 3, 2015 24 / 24
Recommended