OC學習總結(一)

cyxlzzs發表於2015-02-14

OC學習總結(一)

知識要點

1、 類(interface)

》標頭檔案格式.h
@interface class-name : super-class-name <protocol-name,...>{
    @private
    instance variable
    ...
    @public
    ...
    @protected(default)
    ...
}
@property ... //點語法中可以方便使用
//xcode4.4以後property關鍵字實現了三部分工作:成員變數的定義如_name(預設為private),property和synthesize的功能
//構造方法的命名約定(以Person類為例):類方法personWith開頭,例項方法initWith開頭
//+開頭的類方法
//-開頭的例項方法
@end

》實現檔案.m(.mm)
@implementation class-name
    @synthesize ... //點語法中可以方便使用
    //實現方法
    ...
@end

2、 分類(Category)

》標頭檔案格式.h
@interface class-name (category-name)
//+開頭的類方法
//-開頭的例項方法
@end

》實現檔案.m(.mm)
@implementation class-name (category-name)
    //實現方法
    ...
@end

3、 協議(Protocol)

》標頭檔案格式.h
@protocol protocol-name
//協議方法
...
@end

4、 擴充套件(Extension)

》宣告和實現都在.m檔案中
》格式類似於無名Category
@interface class-name ()
@end

》實現在原有類的implementation中
》相當於私有方法,類內部才可用

5、 委託(Delegate)

》協議的一大特色,代理模式

》協議FindHouse的定義
@protocol FindHouse
- (void) findHouse;
@end

》代理中介Agent
@interface Agent : NSObject <FindHouse>
- (void) findHouse;
@end

》甲方Person
@interface Person : NSObject{
    id<FindHouse> delegate; //實現了FindHouse協議的物件都可以作為代理
}
+ (id) personWithDelegate:(id<FindHouse>)pDelegate; //注入代理
- (void) startToFindHouse;
@end

6、 Foundation框架

》NSNumber(涉及到基本資料型別和物件的相互轉化,包裝和拆包)
    包裝[Number numberWithInt:age]
    拆包[number intValue]

》NSString(字串的初始化、拼接、查詢和比較)
    字面量語法 NSString *str = @"hello oc"
    長度[str length]
    子串substringFrom, substringTo, substringWithRange
    比較isEqualToString, compare, caseInsensitiveCompare
    拆分字串 componentsSeparatedByString
    陣列拼接字串componentsJoinedByString
    字串拼接stringWithFormat
    查詢字串hasPrefix, hasSuffix, containsString, rangeOfString

》NSArray(陣列中只能存放物件,包括可變NSMutableArray和不可變陣列)
    建立arrayWithObjects, arrayWithArray
    長度[arr count]
    查詢indexOfObject
    可變陣列addObject

》NSSet(無序列表,只能存放物件,分類可變NSMutableSet和不可變集合,自動去除重複物件)
    建立setWithObjects
    增加setByAddingObject(返回新物件)
    可變集合NSMutableSet
        子集判斷intersectsSet
        交集intersectSet
        差集minusSet

》NSDictionary(字典為鍵值對,鍵值都必須是物件,而且都不能為nil,如果值為空時可採用NSNull)
    建立dictionaryWithObjectsAndKeys
    取值objectForKey
    可變字典 setValue: forKey
    刪除removeObjectForKey
<script type="text/javascript"> $(function () { $('pre.prettyprint code').each(function () { var lines = $(this).text().split('\n').length; var $numbering = $('<ul/>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('<li/>').text(i)); }; $numbering.fadeIn(1700); }); }); </script>

版權宣告:本文為博主原創文章,未經博主允許不得轉載。