Swift4 Day94:Objective-C for Swift Developers

Alice
Daily Swift
Published in
4 min readDec 26, 2018

--

2018.12.26 Objective-C for Swift Developers

  • 建立 String 的方法
NSString *productTitle = @"Bouncey Balls";NSString *manufacturer = @"Acme Novelty";
  • int float double 建立方法
int quantity = 144;float price = 19.9;double unitPrice = quantity/price;
  • Array 的建立方法
身為一個 Swift 工程師你會寫出這樣的 Array
  • 想要把 double 放到Array 必須先將它轉成 NSObject
  • NSNumber NSInteger 不能直接計算,需要轉換
  • print 出 blurb 這個 String
  • 下方的 productTitle ~unitPrice就像在 swift 中的 \() 的用法一樣
  • 注意的是在 字串中用不同的代碼來替代下方的值
    char 字串%c
    int 整數%i
    float 浮點數%f
    double 浮點數%e
  • \r\n 空行

collection types arrays and dictionaries

  • NSArray 與 NSDictionary 是不能修改的,如果要修改要使用 NSMutableDictionary
  • [bostionDict setValue:@”Brazil” forKey:@”Country”];
    是將 Country 的值 從USA 改成 Brazil
  • [bostionDict addEntriesFromDictionary:@{@”Population”:@(656000)}];
    加新的物件到 bostionDict 中
  • 將兩個 NSMutableDictionary 放到 Array 中分別取值的方法
  • alloc 是指 allocating in memory
  • Array 的 addObject/removeObjectAtIndex/replaceObjectAtIndex

conditionals loops operators and more

  • switch 跟 for 的寫法跟 swift 沒什麼差別 但一定要加()

object orientation and classes

  • 新建 NSObjcet 檔案命名為Animal後,會有 h 檔跟 m 檔。
  • .h 為標頭檔,做為宣告屬性及方法使用, .m 為Objective-C檔,做為實際編寫屬性值及方法內容使用。
  • 先定義 Animal.h 的 property
enum Group {Mammal = 0,Bird,Reptile,Amphibian,BonyFish,CarFish};@interface Animal : NSObject@property (nonatomic, strong)NSString *name;@property (nonatomic) enum Group group;@property (nonatomic) BOOL isExtinct;@property (nonatomic) IBOutlet UIImageView *thumbnaiView;@end

classes continued alloc init memory and more

  • 建立一個為 Animal 型別的物件名為 liger
  • 只寫Animal *liger; 會報錯,要記得 alloc

functions and methods

在 Animal.m 中加上 function

讓我們來拆解一下:void 代表沒有回傳值、eat 是 function name、NSStringg 是型別、food 是 parameter

-(void)eat:(NSString*)food{NSLog(@"yum yum, may I have seconds of the delicious %@",food);}

在 Animal.h 中加上介面

-(void)eat:(NSString*)food;

用 [liger eat:@”ceral”]; 在 viewDidLoad 呼叫 function

再加一個 function 到 Animal.m

在 viewDidLoad call profuceOffspring,就可以看到三個物件在 Array中

但沒有 NSMutableArray *litter 也不會顯示,是跟 Swift 比較不同的部分

creating subclasses

建立一個SubClass

Canine.m 中加上 initWithName function 然後Canine.h 加上 interface

如果沒有加 interface 錯誤訊息如下:

No visible @interface for ‘Canine’ declares the selector ‘initWithName:’

-(id) 是指回傳的東西,因為一開始不會檢查型別,所以要小心使用

建立名為 wolf 、類別為 Canine 的物件,

寫一個 bark 的 function MSLog woof woof

Canine.m
-(void)bark{ NSLog(@”woof woof”); }
Canine.h
-(void)bark;

再建立一個繼承 Canine 的 Chihuahua class,會擁有 Canine 的所有能力像 initWithName ,也可以複寫 bark 的內容。

Chihuahua.m
-(void)bark{ NSLog(@”Yip Yip”); }
Chihuahua.h
-(void)bark;

converseWithDog 的 function,Canine 是 parameter 的型別, doggie 則是 parameter 的名字。

Categories

我覺得很像 Swift 中 extension 的用法,先選擇 Objective-C File 在選擇 Category ,Class 選擇 NSString

在 NSString+Translator.m 中,寫下轉換 function ,再去NSString+Translator.h 中設定 interface

-(NSString *)kennethize:(NSString *)phrase{NSMutableString *translatedPhrase = [NSMutableString stringWithString:phrase];[translatedPhrase replaceOccurrencesOfString:@"." withString:@":)" options:0 range:NSMakeRange(0, [translatedPhrase length])];return (NSString*)translatedPhrase;}
把句點改成 :) ,適用在所有 NSString

github 中還有練習 uppercaseString 的範例

Protocols

像上面一樣選擇 Objective-C File 在選擇 protocols ,加 run track 兩個介面 ,ferretOut 則為 optional 的介面。

這跟 Swift 蠻像的,但要注意是 Swift 要用 optional 時要加 @objc

@protocol Hunt <NSObject>-(void)run;-(void)track;@optional-(void)ferretOut;@end

在 Canine.h 中

confirm protocol 1.#import “Hunt.h” 2.Animal<Hunt(protocol's name)>

在Canine.m檔案中就會叫你實做 protocol

也可以去 Chihuahua.m 中實作 optional 的 protocol ferretOut

Github 下載連結:

strings and numerical variables

wl02722691/practice-strings-and-numerical-variabl

collection types arrays and dictionaries

wl02722691/practice-collection-types-arrays-and-dictionaries

object orientation and classes/classes continued alloc init memory and more/functions and methods/creating subclasses/Protocols

wl02722691/practice-object-orientation-and-classes

Categories

wl02722691/practice-categories

參考資料:

--

--