在project開始時,參考使用流程
1. to the directory of root of project
2. find . // to see files
3. echo …/UserInterfaceState.xcuserstate > .gitignore
4. cat .gitignore
5. git init
6. git add .
7. git commit -m "Now under source control"
show
Committer: name <name@chenteki-Mac-Pro.local>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:
git config --global user.name "Your Name"
git config --global user.email you@example.com
After doing this, you may fix the identity used for this commit with:
git commit --amend --reset-author
其他的使用語法參考
http://iphoneipad-develop.blogspot.tw/2011/02/git-branch.html
2012年6月29日 星期五
2012年6月27日 星期三
Foundation Framework基本概念
Xcode 裡面的 Foundation library 提供了許多在開發過程中所會使用到的物件,像是 number, dictionary 等等 ,在視窗環境背後運作的物件,如:字串、數值、陣列、檔案管理、記憶體管理等等。
參考 http://design2u.me/blog/72/iphone-app-%E9%96%8B%E7%99%BC-%E4%BA%8C%E5%8D%81%E5%9B%9B-%E8%AA%8D%E8%AD%98-foundation-framework#more-72
NSObject //最原始的class
Base class for pretty much every object in the iOS SDK
Implements introspection methods, etc.
- (NSString *)description is a useful method to override (it’s %@ in NSLog()).
- (id)copy; // not all objects implement mechanism (raises exception if not)
- (id)mutableCopy; // not all objects implement mechanism (raises exception if not)
NSString
(1) 用 unicode 編碼
(2) 普遍被使用且可以取代 C 乙面的 char *
(3) 如果用 @”foo” 這類語法就會自動產生一個 NSString object
(4) NSString 是無法被修改的
(5) 擁有一些字串的功能, 像是大小寫轉換, URL, 子字串, 型態轉換等等
NSMutableString
(1) NSString 的可變版
(2) 黃字為例子
NSNumber
NSNumber 是一個類別,專門存放 int float double 等等變數,原因是相對於基本型別(int 等等),較好執行記憶體管理,且數字集合的內容也需要是個物件。下面有一個簡單的範例
NSValue
是個很抽象的型態,可以把一些非物件的型態包進去,像是 Pointer, Rect, Size 等等,你可以從 API 來看看它的用法
NSData
NSData 負責存取與傳送 local 的小 Data
NSDate
負責存取日期的物件
NSArray
可以把數個物件串成 array 型態
但是在物件放進去之後就不能再進行新增及刪除
下面四個黃色的是可用的 function
NSMutableArray
跟 Array 很像,但是內容可以改變
可以用 Add 的方式加在最後面或是 insert 在某個位置
NSDictionary
將 Object 串成 Dictionary 的格式,內容一旦被建立就不可以被改變
可以用 count 來算數量, 或是用 key 找物件
NSMutableDictionary
可以增加/刪除物件的 dictionary
NSSet
NSSet 跟 NSArray 有點像,但是沒有順序性,較適合用來做為判斷兩個 SET 之聯集交集,或是是否存在某個原素時使用,不可隨意刪除與增加物件
MSMutableSet
與 MSSet 類似,不過可以新增刪除物件
Enumeration
列舉機制,可以有效率的列舉出 Array 裡面j物件
上面第二張圖就是根據 key 列舉出 dictionary 裡面有的 Object
NSOrderedSet
Immutable ordered collection of distinct objects.
Sort of a hybrid between an array and a set.
Faster to check “contains” than an array, but can’t store an (isEqual:) object multiple times.
Not a subclass of NSSet! But contains most of its methods plus ...
- (int)indexOfObject:(id)anObject;
- (id)objectAtIndex:(int)anIndex;
- (id)firstObject; and - (id)lastObject;
- (NSArray *)array;
- (NSSet *)set;
NSMutableOrderedSet
Mutable version of NSOrderedSet.
- (void)insertObject:(id)anObject atIndex:(int)anIndex;
- (void)removeObject:(id)anObject;
- (void)setObject:(id)anObject atIndex:(int)anIndex;
2012年6月26日 星期二
delegate的定義
參考:
http://blog.csdn.net/pinklpig/article/details/7093796
http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/CommunicateWithObjects.html#//apple_ref/doc/uid/TP40002974-CH7-SW18
delegate翻译成什么还是很重要的,(delegate很多翻译成代理,中文的代理和delegate要表达的意思,有差别。这么翻译,只会增加学习者的难度)。
delegator:授权人
delegate:执行授权
在Objective-c 中,不同对象间通信貌似只能通过protocol & delegate 实现。
使用 Delegate 作通信用途的 5 Steps
1. Create the @protocol
2. Add delegate @property to delegator’s public @interface
3. Use delegate property inside delegator’s implementation
4. Set the delegate property somewhere inside the delegate’s @implementation
5. Implement the protocol’s method(s) in the delegate (include <> on @interface)
sample code:
需要发送消息给其他类的类头文件中:
@protocol ShakeDelegate <nsobject>
@optional
//- (void)shakeAnimationStart;
- (void)callShakeAnimationStart;
- (void)callShakeAnimationStop;
@end
该类中添加变量:
id <shakedelegate> delegate;
M文件中发送信息:
[self.delegate callShakeAnimationStart];
在需要接受信息的类中:
@interface 添加继承
<ShakeDelegate>
设置需要传递的类的instance代理对象为自己:
childInstance.delegate = self;
在自己类实现delegate 方法:
- (void)callShakeAnimationStop{
// oops , i received the message from other class ....
}
完成。
class and instance method
Class Method
Starts with a plus sign
+ (id) alloc;
+ (Ship *)motherShip;
+ (NSString *)stringWithFormat:...
+(void) doSomething;
Creation & Utility Methods
Calling syntax
[Class method]
Ship *ship = [Ship motherShip];
NSString *resultString = [NSString stringWithFormat:@“%g”, result];
[[ship class] doSomething];// 呼叫方法
self/super is this class
self means “this class’s class methods”
super means “this class’s superclass’s class methods”
在使用上,self與super都是 class,因此呼叫的都是class的method
self只是一個pointer指向你自己。
甚麼時候使用class method
1. 創建 instance/object
2. share instance
3. get information from class
class是沒有類似instance的屬性及變數。
Instance method
Starts with a dash (-)
- (BOOL)dropBomb:(Bomb *)bomb
at:(CGPoint)position
from:(double)altitude;
“Normal” Instance Methods
Calling syntax
[<pointer to instance> method]
Ship *ship = ...; // instance of a Ship
destroyed = [ship dropBomb:firecracker
at:dropPoint
from:300.0];
self/super is calling instance
self means “my implementation”
super means “my superclass’s implementation”
[self greeting]; // 呼叫方法, 在同一檔案內, xcode的標準寫法
與 Class Method 的差別,在於
- 號,代表這是一個 Instance Method。
其他則與 Class Method 相同。
另外,要使用 Instance Method,必須先產生 Instance / Object (實體 / 物件)。
實體呼應instance method
- (id) init; // e.g. [[Hello alloc ] init ] <---需要初始設定
- (void) greeting:(NSString *) word;
- (id) init; // e.g. [[Hello alloc ] init ] <---需要初始設定
- (void) greeting:(NSString *) word;
類別呼應class method
+ (id) alloc; // e.g. [Hello alloc]
+ imageNamed:(UIImage) image;
// [UIImage imageNamed:@”hello.png”]
Example
@interface Vehicle // Class
- (void)move;
@end
@interface Ship : Vehicle // class inherit
- (void)shoot;
@end
Ship *s = [[Ship alloc] init];
[s shoot];
[s move];
2012年6月25日 星期一
The Dynamism of Objective-C
參考http://blog.csdn.net/ibright/article/details/6862226
Objective-C有3个动态特性
Objective-C有3个动态特性
1,动态类型
Dynamic typing—determining the class of an object at runtime
运行时决定对象类型
2,动态绑定
Dynamic binding—determining the method to invoke at runtime
运行时决定方法调用
3,动态加载
Dynamic loading—adding new modules to a program at runtime
运行时加载新模块
(isa 指針) and (id) in Dynamic typing
蘋果原始資料
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocObjectsClasses.html
isa 指針: NSObject 中有一個 Class isa 的指針類型的成員變量,因為我們的對象大都直接或者間接的從 NSObject 繼承而來,因此都會繼承這個 isa 成員變量,isa 在運行時會指向對象的 Class 對象, 一個類的所有對象的 Class 對象都是同一個(JAVA 也是如此),這保證了在內存中每一個類 型都有唯一的類型描述。這個 Class 對象中也有個 isa 指針,它指向了上一級的父類的 Class 對象。 在明白了這個 isa 之後,你就可以明白在繼承的時候,A extends B,你調用 A 的方法 a(),首 先 A 的 isa 到 A 的 Class 對象中去查找 a()方法,找到了就調用,如果沒找到,就驅使 A 的 Class 對象中的 isa 到父類 B 的 Class 對象中去查找。
參考 http://www.guan8.net/Java/1164462.html
Objective-C一個特別的data type。id就是:
與下面的寫法同義
nil 即為null object,也就是id值為0,id、nil及其他basic type object都在objc/objc.h裡面。
任何 Object 不論其類別為何,在 Runtime 時才會 allocate memory 並由 isa 來決定它真正的類別,而非如同 C++ 在 compile time 時,就安排好不同類別的記憶體配置。
這種不指定特定類別的方式其實就替"未來"可能有新的類別保留了非常大的彈性,也不需要特別以 design pattern 來花太多心思設計 abstract class (interface),對於寫程式的人來說可以很直覺。
PS. 這種 Dynamic typing 與 ECMAScript 中的 prototype chain 設計原理相同,但 ECMAScript 則有更大的彈性,可以在 Runtime 時任意改變其 prototype;雖然理論上 Objective-C 也可以辦得到,但目前似乎沒有這樣的設計。
參考 http://愛瘋手機.tw/node/4664-2009-08-11.htm
http://rintarou.dyndns.org/2010/12/09/objective-c-%E6%B7%BA%E8%AB%87/
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocObjectsClasses.html
isa 指針: NSObject 中有一個 Class isa 的指針類型的成員變量,因為我們的對象大都直接或者間接的從 NSObject 繼承而來,因此都會繼承這個 isa 成員變量,isa 在運行時會指向對象的 Class 對象, 一個類的所有對象的 Class 對象都是同一個(JAVA 也是如此),這保證了在內存中每一個類 型都有唯一的類型描述。這個 Class 對象中也有個 isa 指針,它指向了上一級的父類的 Class 對象。 在明白了這個 isa 之後,你就可以明白在繼承的時候,A extends B,你調用 A 的方法 a(),首 先 A 的 isa 到 A 的 Class 對象中去查找 a()方法,找到了就調用,如果沒找到,就驅使 A 的 Class 對象中的 isa 到父類 B 的 Class 對象中去查找。
參考 http://www.guan8.net/Java/1164462.html
Objective-C一個特別的data type。id就是:
typedef struct objc_object {基本設計理念就是:不管任何型態的 Object 都是由 pointer 來指定,這也就是為什麼在 Objective-C 裡,任何 Object variable 的宣告都是 pointer (*),只有 primitive data type 以及 C struct 是例外。
Class isa;} *id;
而Class本身就是個pointer:
typedef struct objc_class *Class;所以isa稱為isa pointer。
至此,我們可定義一個id object如下:
id anObject;
與下面的寫法同義
#import "MyObject.h" MyObject *anObject;
不同之處在於若寫成 MyObject *
anObject; 時,compiler 能夠從 MyObject.h 得知 MyObject class 的宣告內容,而在 compile 時幫忙檢查對於 myobj 的存取是否符合定義。nil 即為null object,也就是id值為0,id、nil及其他basic type object都在objc/objc.h裡面。
objects均是 dynamically typing,也就是說,在程式執行時(run time)才最後決定該object的type。Dynamic typing 也就是直到 Runtime 時才來決定 Object 究竟為何種 class,最重要的就 是 (id) 的設計
任何 Object 不論其類別為何,在 Runtime 時才會 allocate memory 並由 isa 來決定它真正的類別,而非如同 C++ 在 compile time 時,就安排好不同類別的記憶體配置。
這種不指定特定類別的方式其實就替"未來"可能有新的類別保留了非常大的彈性,也不需要特別以 design pattern 來花太多心思設計 abstract class (interface),對於寫程式的人來說可以很直覺。
PS. 這種 Dynamic typing 與 ECMAScript 中的 prototype chain 設計原理相同,但 ECMAScript 則有更大的彈性,可以在 Runtime 時任意改變其 prototype;雖然理論上 Objective-C 也可以辦得到,但目前似乎沒有這樣的設計。
參考 http://愛瘋手機.tw/node/4664-2009-08-11.htm
http://rintarou.dyndns.org/2010/12/09/objective-c-%E6%B7%BA%E8%AB%87/
Introspection
參考:http://psvsps2.blogspot.tw/2009/07/cocoa-fundamental-iphone-part2_31.html
動態型別(Dynamically type)和靜態型別(Statically Type)的優劣爭論持續了好一陣子, 動態型別程式語言帶來的靈活性,靜態型別的嚴謹各有千秋,動態型別表現傑出,幾個重要的程式語言如 python , ruby 都有相當亮眼的表現,Objective-C 也是一種動態型別的程式語言,通常動態型別程式語言會提供比較多的自省(introspection)能力,如 runtime 檢查type,檢查method等等。
Objective-C’s runtime support allows you to discover various properties of objects at
execution time. This process is called introspection. You can find out an object’s class and
superclass using the methods class and superclass, as follows:
Class objectsClass;
Class objectsSuperClass;
id anObject;
objectsClass = [anObject class];
objectsSuperClass = [anObject superclass];
蘋果原始資料
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocObjectsClasses.html
Introspection refers to the inherent ability of an object to divulge, upon request, its essential characteristics at runtime. By sending objects certain messages, you can ask them questions about themselves as objects and the Objective-C runtime provides you with answers. Introspection is an important coding tool because it makes your programs more efficient and robust.
因為所有的class都繼承自NSObject,因此有三個方便來辨認現在的狀態
All objects that inherit from NSObject know these methodsisKindOfClass: returns whether an object is that kind of class (inheritance included)
isMemberOfClass: returns whether an object is that kind of class (no inheritance)
respondsToSelector: returns whether an object responds to a given method
例如
You get a Class by sending the class method class to a class :)
if ([obj isKindOfClass:[NSString class]]) {
NSString *s = [(NSString *)obj stringByAppendingString:@”xyzzy”];
}
Types of Introspection Information
TheNSObject protocol, which is adopted by the NSObject class, defines introspection methods that yield the following kinds of information about an object:- Class membership. To determine if an object inherits, directly or indirectly, from a particular class, send it an
isKindOfClass:message and evaluate the result. This method tells you if the object is a direct instance of the given class. You can also use theclassandsuperclassmethods to obtain the class or superclass of an object and then use that result in comparison operations. - Messages responded to. To find out if an object’s class or superclass implements a method, send the object a
respondsToSelector:message. The parameter is aSEL-typed value constructed from the signature of the method using the@selectordirective. For example:
BOOL doesRespond = [anObject respondsToSelector:@selector(writeToFile:atomically:)];
- Protocol conformance.
If a class conforms to a formal protocol, you can expect it to
implement the required methods of that protocol and send messages to it
accordingly. Use the
conformsToProtocol:method to obtain this information. You specify the argument of this method using the@protocoldirective.
訂閱:
文章 (Atom)
