麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發(fā)設(shè)計 > 正文

(4/18)重學Standford_iOS7開發(fā)_框架和帶屬性字符串_課程筆記

2019-11-14 18:57:49
字體:
供稿:網(wǎng)友

第四課(干貨課):

  (最近要復習考試,有點略跟不上節(jié)奏,這節(jié)課的內(nèi)容還是比較重要的,仔細理解掌握對今后的編程會有很大影響)

  本節(jié)課主要涉及到Foundation和UIKit框架,基本都是概念與API知識,作者主要做一歸納整理。

  0、其他

    a.對象初始化

      ①通過alloc init(例如[[NSString alloc] initWithFormat:@"%d",2])

      ②通過類方法(例如[NSString StringWithFormat:@"%d",2])

     ?、弁ㄟ^其他實例對象的方法(例如stringByAppendingString:)

    b.nil

      可以給nil發(fā)送消息,但只會得到nil

      對于返回為struct類型的方法會返回未定義的類型

    c.動態(tài)綁定

      對象在執(zhí)行期間(runtime)才會判斷所引用對象的實際類型.

      id(實質(zhì)上為所有對象指針的類型如NSString *),這里討論動態(tài)綁定主要為了明確哪些情況會出現(xiàn)編譯警告與運行崩潰,方便后面討論統(tǒng)一概念。

      課程中的原例子:

1 @interface Vehicle2 - (void)move;3 @end4 @interface Ship : Vehicle5 - (void)shoot;6 @end

      情況①:屬于正常使用情況,不會出現(xiàn)編譯警告與崩潰。

 1 Ship *s = [[Ship alloc] init]; 2 [s shoot]; 3 [s move]; 

      情況②:父類調(diào)用子類特有方法,編譯時會有警告,運行時正常運行。

 1 Vehicle *v = s; 2 [v shoot]; 

      情況③:任意id類型調(diào)用子類方法,無編譯警告(因為類型為id),運行時若obj不為ship類,則會崩潰。若調(diào)用不存在的方法,則會出現(xiàn)編譯警告(盡管類型為obj)

 1 id obj = ...; 2 [obj shoot]; 3 [obj someMethodNameThatNoObjectAnywhereRespondsTo]; 

       請況④:其他類型調(diào)用子類方法,會出現(xiàn)編譯警告,若進行類型轉(zhuǎn)換則沒有編譯警告,但仍會崩潰。

1 NSString *hello = @”hello”;2 [hello shoot];3 Ship *helloShip = (Ship *)hello;4 [helloShip shoot];5 [(id)hello shoot];

     動態(tài)綁定的問題可能會引發(fā)嚴重的錯誤(如毫無節(jié)制的類型轉(zhuǎn)換等)

     解決動態(tài)綁定問題的思路:內(nèi)省機制,協(xié)議

     內(nèi)?。篘SObject基類提供了一系列的方法如isKindOfClass:(是否為這個類或其子類的實例),isMemberOfClass:(是否為這個類的實例),respondsToSelector:(對象是否響應某方法)來在運行時檢測對象的類型。

     檢測方法的變量為選擇器selector(SEL),使用方法如下:

1 if ([obj respondsToSelector:@selector(shoot)]) {2     [obj shoot];3 } else if ([obj respondsToSelector:@selector(shootAt:)]) {4     [obj shootAt:target];5 }6 7 SEL shootSelector = @selector(shoot);8 SEL shootAtSelector = @selector(shootAt:);9 SEL moveToSelector = @selector(moveTo:withPenColor:);
1 [obj performSelector:shootSelector];2 [obj performSelector:shootAtSelector withObject:coordinate];3 4 [array makeObjectsPerformSelector:shootSelector]; // 用于數(shù)組,批量對數(shù)組中的對象發(fā)送消息5 [array makeObjectsPerformSelector:shootAtSelector withObject:target]; // target is an id6 7 [button addTarget:self action:@selector(digitPRessed:) ...];//MVC中的target-action

 

   1、Foundation

    a.NSObject

      所有類型的基類,提供了一系列通用的方法。
      - (NSString *)description描述對象內(nèi)容(格式化輸出%@),一般在子類中自實現(xiàn)。

      - (id)copy; // 嘗試復制為不可變對象
      - (id)mutableCopy; // 嘗試復制為可變對象

    b.NSArray

- (NSUInteger)count;- (id)objectAtIndex:(NSUInteger)index; //返回下標為index的數(shù)組元素- (id)lastObject; // 返回數(shù)組末尾元素- (id)firstObject; // 返回數(shù)組頭元素- (NSArray *)sortedArrayUsingSelector:(SEL)aSelector;//使用自定義方法對數(shù)組排序- (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(id)selectorArgument;- (NSString *)componentsJoinedByString:(NSString *)separator;//將數(shù)組轉(zhuǎn)化為字符串并用separator分隔

     c.NSMutableArray

1 + (id)arrayWithCapacity:(NSUInteger)numItems; // numItems is a performance hint only 2 + (id)array; // [NSMutableArray array] is just like [[NSMutableArray alloc] init]3 4 - (void)addObject:(id)object; // 在尾部加入元素5 - (void)insertObject:(id)object atIndex:(NSUInteger)index;//在下標index處插入元素6 - (void)removeObjectAtIndex:(NSUInteger)index;//移除index處元素

     數(shù)組的遍歷:

 1 NSArray *myArray = ...; 2 for (NSString *string in myArray) 3  {  4     // no way for compiler to know what myArray contains 5     double value = [string doubleValue]; // crash here if string is not an NSString  6 } 7  8 NSArray *myArray = ...; for (id obj in myArray) 9  {10     // do something with obj, but make sure you don’t send it a message it does not respond to 11     if ([obj isKindOfClass:[NSString class]]) 12     {13         ?// send NSString messages to obj with no worries14      }15 }    

     d.NSNumber

      對常用基本類型的封裝

1 NSNumber *n = [NSNumber numberWithInt:36];2 float f = [n floatValue];3 4 //便利初始化方式5 NSNumber *three = @3;6 NSNumber *underline = @(NSUnderlineStyleSingle); // enum7 NSNumber *match = @([card match:@[otherCard]]); 

     e.其他簡單類型

      NSValue、NSData、NSDate(NSCalendar,NSDataFormatter,NSDateComponents)、NSSet(NSMutableSet)、NSOrderedSet(NSMutableOrderedSet)

     f.NSDictionary

//初始化方式@{ key1 : value1, key2 : value2, key3 : value3 }//查表方式UIColor *colorObject = colors[colorString]; //常用方法- (NSUInteger)count;- (id)objectForKey:(id)key;

    g.NSMutableDictionary

1 //常用方法2 - (void)setObject:(id)anObject forKey:(id)key;//添加鍵值3 - (void)removeObjectForKey:(id)key;//移除鍵值4 - (void)removeAllObjects;5 ?- (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary;//合并字典
1 //遍歷方式2 NSDictionary *myDictionary = ...;3    for (id key in myDictionary)4  {5     // do something with key here6     id value = [myDictionary objectForKey:key];7     // do something with value here 8 }

    h.屬性列表

    屬性列表是一種保存數(shù)據(jù)的方式,定義為集合的集合,可以為NSArray, NSDictionary, NSNumber, NSString, NSDate, NSData。

    可以對上述對象直接發(fā)送- (void)writeToFile:(NSString *)path atomically:(BOOL)atom;消息保存為屬性列表文件

    i.NSUserDefaults

    輕量級的本地數(shù)據(jù)存儲

[[NSUserDefaults standardUserDefaults] setArray:rvArray forKey:@“RecentlyViewed”];//常用方法- (void)setDouble:(double)aDouble forKey:(NSString *)key;- (NSInteger)integerForKey:(NSString *)key; // NSInteger is a typedef to 32 or 64 bit int - (void)setObject:(id)obj forKey:(NSString *)key; // obj must be a Property List- (NSArray *)arrayForKey:(NSString *)key; // will return nil if value for key is not NSArray//保存完畢后必須進行同步[[NSUserDefaults standardUserDefaults] synchronize];

    j.NSRange

1 typedef struct {2     NSUInteger location;3     NSUInteger length;4 } NSRange;5 6 //創(chuàng)建7 NSMakeRange(NSUInteger location,NSUInteger length);

  2、UIKit

    a.UIColor

//系統(tǒng)內(nèi)置顏色[UIColor blackColor];[UIColor blueColor];[UIColor greenColor];...//RGB顏色+ (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha;//alpha為透明度//HSB顏色+ (UIColor *)colorWithHue:(CGFloat)hue saturation:(CGFloat)saturation brightness:(CGFloat)brightness alpha:(CGFloat)alpha;

    b.UIFont

//系統(tǒng)字體UIKIT_EXTERN NSString *const UIFontTextStyleHeadline NS_AVAILABLE_IOS(7_0);UIKIT_EXTERN NSString *const UIFontTextStyleBody NS_AVAILABLE_IOS(7_0);UIKIT_EXTERN NSString *const UIFontTextStyleSubheadline NS_AVAILABLE_IOS(7_0);UIKIT_EXTERN NSString *const UIFontTextStyleFootnote NS_AVAILABLE_IOS(7_0);UIKIT_EXTERN NSString *const UIFontTextStyleCaption1 NS_AVAILABLE_IOS(7_0);UIKIT_EXTERN NSString *const UIFontTextStyleCaption2 NS_AVAILABLE_IOS(7_0);//新建字體UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];//常用方法+ (UIFont *)systemFontOfSize:(CGFloat)pointSize;+ (UIFont *)boldSystemFontOfSize:(CGFloat)pointSize;

    c.UIFontDescriptor

 1 //創(chuàng)建一個字體 2 + (UIFont *)fontWithDescriptor:(UIFontDescriptor *)descriptor size:(CGFloat)size; 3  4 //使用舉例 5 UIFont *bodyFont = [UIFont preferredFontForTextStyle:UIFontTextStyleBody]; 6 UIFontDescriptor *existingDescriptor = [bodyFont fontDescriptor]; 7 UIFontDescriptorSymbolicTraits traits = existingDescriptor.symbolicTraits; 8 traits |= UIFontDescriptorTraitBold; 9 UIFontDescriptor *newDescriptor = [existingDescriptor fontDescriptorWithSymbolicTraits:traits];10 UIFont *boldBodyFont = [UIFont fontWithFontDescriptor:newDescriptor size:0];

    d.Attributed Strings

     ?、貼SAttributeString

        帶屬性的字符串(不是字符串),可通過字典設(shè)置一系列字符屬性。

//獲取特定范圍的字符屬性- (NSDictionary *)attributesAtIndex:(NSUInteger)index effectiveRange:(NSRangePointer)range;//獲取對應字符串- (NSString *)string;

      ?、贜SMutableAttributeString

//常用的動態(tài)設(shè)定屬性的方法- (void)addAttributes:(NSDictionary *)attributes range:(NSRange)range;- (void)setAttributes:(NSDictionary *)attributes range:(NSRange)range;- (void)removeAttribute:(NSString *)attributeName range:(NSRange)range;//轉(zhuǎn)化為可變字符串- (NSMutableString *)mutableString
//舉例UIColor *yellow = [UIColor yellowColor];UIColor *transparentYellow = [yellow colorWithAlphaComponent:0.3];//字符串屬性字典 @{ NSFontAttributeName :      [UIFont preferredFontWithTextStyle:UIFontTextStyleHeadline]   NSForegroundColorAttributeName : [UIColor greenColor],   NSStrokeWidthAttributeName : @-5,   NSStrokeColorAttributeName : [UIColor redColor],   NSUnderlineStyleAttributeName : @(NSUnderlineStyleNone),   NSBackgroundColorAttributeName : transparentYellow }
1 //for UIButton2 - (void)setAttributedTitle:(NSAttributedString *)title forState:...;3 //for UILable4 @property (nonatomic, strong) NSAttributedString *attributedText;5 //for UITextView6 @property (nonatomic, readonly) NSTextStorage *textStorage;

   3、作業(yè)

    無

 

  其它:本節(jié)課主要是理論鋪墊,著重API的講解,都是很常用的對象,因此務必做到熟練使用,在今后的iOS開發(fā)中才不會出現(xiàn)基礎(chǔ)問題。希望大家可以多多查閱文檔,實際編程時重點理解動態(tài)綁定等概念。

 

課程視頻地址:網(wǎng)易公開課:http://open.163.com/movie/2014/1/B/P/M9H7S9F1H_M9H7VPRBP.html

       或者iTunes U搜索standford課程


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 国产成年人在线观看 | 大学生一级毛片在线视频 | 久久艹精品 | 久国久产久精永久网页 | 涩涩激情网 | 久久亚洲视频网 | china对白普通话xxxx | 欧美a视频 | 色综合视频网 | 精品一区二区免费 | 欧美毛片在线观看 | 久草视频2 | 日韩视频在线不卡 | 久久久久久久久久久亚洲 | 国产做爰全免费的视频黑人 | 99国产精品国产免费观看 | 欧美成人二区 | 婷婷久久影院 | 成人在线影视 | 免费在线观看午夜视频 | 黄视频网站免费在线观看 | 久久久久中精品中文字幕19 | 久久精品一区二区三区国产主播 | 欧美成年人在线视频 | 12av电影 | 国产成人在线免费视频 | 视频一区二区三区在线播放 | 日本高清无遮挡 | 色淫网站免费视频 | 色综合狠狠 | 精品一区二区三区在线观看国产 | 一级网站 | 久草高清视频 | 俄罗斯hdxxx| h视频免费在线观看 | 色婷婷久久久亚洲一区二区三区 | 国产做爰全免费的视频黑人 | 国产剧情在线观看一区二区 | 97久久精品一区二区三区观看 | 国产精品一区在线观看 | 爱福利视频网 |