Learn Objective C(3)Programming with Objective-C - Defining Classes, Working with Objects
About Objetive-C
Objects package data with related behavior.
We will not build our own app from scratch, instead we have a large library of existing objects available for our use, provided by Cocoa(for OS X) and Cocoa Touch(for iOS).
1. Defining Classes
1.1 Classes Are Blueprints for Objects
NSString, NSNumber
Mutability Determines Whether a Represented Value Can Be Changed
NSString ---> NSMutableString
Classes Inherit from Other Classes
NSMutableString is inherited from NSString
The Root Class Provides Base Functionality
At least, we inherit from NSObject.
NSObject --> UIResponder -->UIView --> UIControl --> UIButton
1.2. The Interface for a Class Defines Expected Interacctions
Basic Syntax
@interface SimpleClass : NSObject
…snip...
@end
All the public properties and behavior are defined inside the @interface declaration.
Properties Control Access to an Object's Values
@interface Person:NSObject
@property NSString *firstName;
@property NSString *lastName;
@property NSNumber *yearOfBirth;n // One alternative would be @property int yearOfBirth;
@end
Put an asterisk in front of Objective-C objects to indicate that they are C pointers. And put a semi-colon at the end.
Property Attributes Indicate Data Accessibility and Storage Considerations
Make something read only.
…snip…
@property (readonly) NSString *firstName;
@property (readonly) NSString *lastName; //Property attributes are specified inside parentheses
…snip…
Method Declarations Indicate the Messages an Object Can Receive
Method on that object --- sending a message to another object by calling a method on that object
- (void) someMethod;
The minus sign(-) indicates that it is an instance method. It is different from class methods.
Methods Can Take Parameters
colons are used in the parameters.
-(void) someMethodWithValue:(SomeType)value;
-(void) someMethodWithFirstValue:(SomeType)value1 secondValue:(AnotherType)value2;
Class Names Must Be Unique
@interface ZJUPerson:NSObject
…snip…
@end
Two-letter prefixes like NS and UI(for User Interface elements on iOS) are reserved for use by Apple.
1.3. The Implementation of a Class Provides Its Internal Behavior
Basic Syntax
#iimport "ZJUPerson.h"
@implementation ZJUPerson
..snip..
@end
Implementing Methods
@interface ZJUPerson:NSObject
-(void)saySomething;
@end
#import "ZJUPerson.h"
@implementation ZJUPerson
-(void)saySomething{
NSLog(@"something");
}
@end
Method names should begin with a lowercase letter. We suggest use camel case.
1.4. Objective-C Classes Are also Objects
…snip...
2. Working with Objects
2.1 Objects Send and Receive Messages
square brackets [someObject doSomething];
Use Pointers to Keep Track of Objects
Objective-C use variables to keep track of values.
When a local scalar variable (int or float) goes away when the execution reaches the closing brace of the method, the value disappears too.
-(void) myMethod{
int someInteger = 42;
}
An object's memory is allocated and deallocated dynamically. A local variable is allocated on the stack, while objects are allocated on the heap.
This requires you to use C pointers (which hold memory addresses) to keep track of their location in memory.
-(void) myMethod {
NSString *myString = @"asdfsadf"
..snip..
}
Although the scope of the pointer variable myString is limited to the scope of myMethod, the actual string object that it points to in memory may have a longer life outside that scope.
You Can Pass Objects for Method Parameters
- (void) saySomething:(NSString *) greeting {
NSLog(@"%@", greeting);
}
Methods Can Return Values
- (int) magicNumber {
return 42;
}
tracking the return value
int interestingNumber = [someObject magicNumber];
The same thing for Object.
- (NSString *) uppercaseString;
NSString *testString = @"Hello, world!";
NSString *revisedString = [testString uppercaseString];
Automatic Reference Counting(ARC) feature of the Objective-C compiler takes care of these considerations for memory management.
Objects Can Send Messages to Themselves
@implementation ZJUPerson
-(void) sayHello{
[self saySomething:@"Hello,world"];
}
-(void) saySomething:(NSString *)greeting{
NSLog(@"%@", greeting);
}
@end
self is also a pointer which points to itself.
Objects Can Call Methods Implemented by Their Superclasses
super -- self
2.2 Objects Are Created Dynamically
The NSObject root class provides a class method, alloc
+ (id) alloc;
Keyword id means 'some kind of object' in Objective-C. It is a pointer to an object, like (NSObject *)
+ (id) alloc; //make sure we have enough memory
- (id) init; //make sure we have the right initial values
NSObject *newObject = [[NSObject alloc] init];
Initializer Methods Can Take Arguments
- (id)initWithInt:(int)value;
NSNumber *magicNumber = [[NSNumber alloc] initWithInt:42];
Class Factory Methods Are an Alternative to Allocation and Initialization
+ (NSNumber *) numberWithInt:(int)value;
NSNumber *magicNumber = [NSNumber numberWithInt:42];
Use new to Create an Object If No Arguments Are Needed for Initialization
It is effectively the same as calling alloc and init with no arguments.
ZJUObject *object = [ZJUObject new];
or alternative
ZJUObject *object = [[ZJUObject alloc ] init ];
Literals Offer a Concise Object-Creation Syntax
NSString *something = @"asdfasdf";
NSNumber *myBool = @YES;
NSNumber *myFloat = @3.14f;
NSNumber *myInt = @42;
NSNumber *myLong = @42L;
2.3 Objective-C Is a Dynamic Language
Determining Equality of Objects
if( someInteger == 42) …
When dealing with objects, the == operator is used to test whether two separate pointers are pointing to the same object.
So if we want to test whether two objects represent the same data, we need to call a method like isEqual: available from NSObject.
if ([firstPerson isEqual: secondPerson ])
Working with nil
BOOL success = NO;
int magicNumber = 42;
ZJUPerson *somePerson; // automatically set to nil.
Check nil, for instance
if(somePerson != nil) or if(somePerson)
References:
https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/Introduction/Introduction.html
- 浏览: 2539709 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
发表评论
-
ionic UI(5)UI and Backend
2016-12-02 03:22 589ionic UI(5)UI and Backend 1 Pr ... -
Stanford Cource(2)Demo App Caculator
2014-06-24 01:29 896Stanford Cource(2)Demo App Ca ... -
Mono on MAC
2014-06-04 03:27 973Mono on MACJust fine the tool f ... -
IOS7 App Development Essentials(4)IPhone5, IPhone5s, IPhone5c
2014-04-11 03:59 980IOS7 App Development Essentia ... -
IOS7 App Development Essentials(3)NSUserDefaults
2014-04-11 02:58 1005IOS7 App Development Essentia ... -
IPhone and Location(2)Documents Region Monitoring and Region Sample
2013-10-18 05:10 1725IPhone and Location(2)Documents ... -
IPhone and Location(1)Documents User Location
2013-10-18 03:50 1303IPhone and Location(1)Documents ... -
Learn Objective C(6)Programming with Objective-C - Working with Blocks and Deali
2013-10-18 00:02 923Learn Objective C(6)Programming ... -
Learn Objective C(5)Programming with Objective-C - Working with Protocols and Va
2013-10-17 23:47 995Learn Objective C(5)Programming ... -
Learn Objective C(4)Programming with Objective-C - Encapsulating Data and Custom
2013-10-17 23:23 933Learn Objective C(4)Programming ... -
Learn Objective C(2)Learn Objective-C in Day 6 - 4 ~ 6
2013-10-17 00:30 959Learn Objective C(2)Learn Obj ... -
Learn Object C(1) Learn Objective-C in Day 6 - 1 ~ 3
2013-10-17 00:22 1108Learn Object C(1) Learn Objec ... -
APNS(4)Recall the Process and Learn Java APNS
2013-04-18 02:48 3466APNS(4)Recall the Process and L ... -
Build the iOS Things with J2Objc
2013-04-12 03:25 2461Build the iOS Things with J2Obj ... -
APNS(3)Write the Easy Client App
2013-01-15 07:23 1691APNS(3)Write the Easy Client Ap ... -
APNS(2)Try to Finish the first Example
2013-01-14 07:56 1536APNS(2)Try to Finish the first ... -
Stanford Cource(1)MVC and Object-C
2012-12-14 14:04 1315Stanford Cource(1)MVC and Objec ... -
Some VI Tips
2012-11-15 04:48 1091Some VI Tips Today, I need to c ... -
MAC Mini Setup
2012-09-25 18:45 1325MAC Mini Setup I am dealing wit ... -
Android Talker(1)MAC Environment
2012-09-01 00:16 1920Android Talker(1)MAC Environmen ...
相关推荐
学习Objective-C之前,建议具备一定的C语言基础,了解基本的数据类型、控制流结构和指针操作。 ### See Also 参考文档和教程对于学习Objective-C非常有帮助。官方文档提供了详细的API参考和最佳实践指南。 ### ...
to implement a variety of interesting techniques and special effects, such as working with animated character meshes, picking, environment mapping, normal mapping, real-time shadows, and ambient ...
Introduction to 3D Game Programming with DirectX 9.0c Shader Approach 源代码 This book presents an introduction to programming interactive computer graphics, with an emphasis on game development, ...
This book presents an introduction to programming interactive computer graphics, with an emphasis on game development, using Direct3D 10. It teaches the fundamentals of Direct3D and shader programming...
This book presents an introduction to programming interactive computer graphics, with an emphasis on game development, using real-time shaders with DirectX 9.0c. It teaches the fundamentals of Direct...
This book presents an introduction to programming interactive computer graphics, with an emphasis on game development, using Direct3D 10. It teaches the fundamentals of Direct3D and shader programming...
The chapter begins with an overview of the fundamental concepts of variables and data types in C programming. It introduces the basic syntax and semantics of declaring and initializing variables. ###...
- **Objects, Classes, and Messaging(对象、类与消息传递)**:深入探讨了Objective-C中的核心概念——对象、类以及消息传递机制,这是Objective-C的基础。 #### 三、对象与消息传递 - **Objects(对象)** - *...
This book presents an introduction to programming interactive computer graphics, with an emphasis on game development, using real-time shaders with DirectX 9.0c. It teaches the fundamentals of Direct...
Mastering Machine Learning with scikit-learn (2 ed) (True PDF + AWZ3 + codes) Table of Contents Preface 1 Chapter 1: The Fundamentals of Machine Learning 6 Defining machine learning 6 Learning from ...
This book presents an introduction to programming interactive computer graphics, with an emphasis on game development, using real-time shaders with DirectX 9.0c. It teaches the fundamentals of Direct...
statements, loops, and functions, before moving into defining classes. Students learn basic logic and programming concepts before moving into object-oriented programming, and GUI programming. Another ...
### 关于Objective-C ...以上是对Objective-C基础概念和编程技巧的详细介绍,这些知识点对于理解和使用Objective-C语言至关重要。通过掌握这些概念和技术,开发者可以更高效地构建高质量的应用程序。
在Matlab中定义数学函数是进行数值计算和数据分析的基础。本教程主要涵盖了如何在Matlab环境中创建和使用自定义函数,以及如何优化这些函数以提高效率。以下是对这个主题的详细探讨: 首先,Matlab中的函数是通过...
Title: R Object-Oriented Programming Author: Kelly Black Length: 190 pages Edition: 1 Language: English Publisher: Packt Publishing Publication Date: 2014-10-23 ISBN-10: 1783986689 ISBN-13: ...