fb_license

技術標籤

@selector (1) 初使化區塊 (1) 物件 (1) 物件導向 (2) 型別 (4) 封裝 (1) 流程控制 (1) 陣列 (3) 推論型別 (2) 實機測試 (1) 蓋索林(gasolin) (1) 模組 (1) 憑證 (1) 轉型 (1) 羅康鴻 (121) 類別 (1) 變數 (5) Accelerometer (1) ActiveRecord (1) Activity (1) AFNetworking (1) alloc (1) Android (3) Animation (1) App (1) App ID (1) APP上傳 (1) ASP.NET (1) AVAudioPlayer (1) block (1) C# (2) class (1) CLLocationManager (1) CLLocationManagerDelegate (1) CMMotionManager (4) Controller (1) delegate (1) DELETE語法 (1) Device Motion資料 (1) Dialog (1) DropDownList (1) dynamic language (2) Facebook SDK (9) FBRequest (5) FBRequestConnection (2) FMDB (1) Gesture Recognizers (6) GROUP BY (2) Gyro (1) HAVING (1) IBAction (1) IBOutlet (1) id (3) inheritance (1) init (1) Insert (1) instance variable (1) Interface Builder (1) iOS (70) iOS idea (7) iOS Introduction (1) Layout (1) Magnetometer (1) Menu (2) Method (2) MKMapView (1) MKPointAnnotation (1) MS SQL (5) Nil (1) NSArray (1) NSDictionary (1) NSError (1) NSFileManager & .plist (1) NSMutableArray (1) NSMutableDictionary (1) NSNotificationCenter (1) NULL (1) object (2) Objective-C (16) Objective-C idea (1) ORDER BY (1) Parameter (1) property (1) protocol (2) Provisioning (1) Proximate Sensor (1) Q and A (2) R類別 (1) Rails (9) RESTful SOA (9) Ruby (8) Scene (1) SEELECT (1) Segue (2) SEL (1) SELECT語法 (1) Shake (1) Simulator (1) SOA (8) SQL (6) SQL Server (5) SQL函數 (1) SQL彙總函數 SQL (1) SQLite (1) Storyboard (1) Style (1) Swift (1) Table (1) target & action (1) Theme (1) Toast (1) TRUNCATE TABLE語法 (1) UIActionSheet (1) UIActionSheetDelegate (1) UIActivityIndicatorView (1) UIAlertView (1) UIBarButtonItem (1) UIButton (1) UICollectionView (1) UICollectionViewDataSource (1) UIControl (9) UIDatePicker (1) UIImage (1) UIImagePickerController (2) UIImagePickerControllerDelegate (2) UIImageView (1) UILabel (1) UILongPressGestureRecognizer (1) UINavigationController (2) UIPanGestureRecognizer (1) UIPinchGestureRecognizer (1) UIProgressView (1) UIResponder (1) UIRotationGestureRecognizer (1) UISegmentedControl (1) UISlider (1) UIStepper (1) UISwipeGestureRecognizer (1) UISwitch (1) UITabBarController (1) UITableView (1) UITableViewDataSource (1) UITapGestureRecognizer (1) UITextField (1) UITextFieldDelegate (1) UITextView (2) UITextViewDelegate (1) UIToolBar (1) UIView (8) UIWebView (1) UPDATE語法 (1) var (2) VB.NET (7) View (4) WHERE子句 (1) XML (1)

2014/02/23

[Objective-C] 物件的二階段生成 - alloc & init


idea...



每台iPad都有所謂的出廠設定,比如UDID的資訊,在哪些設定UDID資訊比較好呢?
我是類別class,
其實您可以改寫init()方法,
並於init()中決定您的出場設定。

我是物件Object,
這時除了透過alloc實際產生一台iPad物件外,
還可再透過init進行iPad出廠設定的配置了。




1. 建立iPad類別
  • 新增Objective-C class檔案,取名為iPad
2. 宣告iPad實體變數
  • 分別宣告UDID(裝置識別碼)、Type(機型)、Capacity(容量)等3個iPad資訊
  • 加上@public,開放實體變數的存取權限
#import <Foundation/Foundation.h> @interface iPad : NSObject { @public NSString * UDID; NSString * Type; NSInteger Capacity; } @end

3. 改寫init初始式,設定iPad的UDID出場值
  • 透過#import "iPad.h"引用iPad類別
  • init是initialize的縮寫,代表初始的動作,而init初始式是一個內定即有的方法(後面小節會提供什麼是方法),重新改寫在定義一次就可改寫init初始式執行的內容
  • 透過NSUDID隨機產生iPad的出場值
#import "iPad.h" @implementation iPad - (id)init { //透過NSUUID隨機產生代碼 NSUUID *UUID = [NSUUID UUID]; NSString* stringUUID = [UUID UUIDString]; //並設定為iPAD的UDID self->UDID = stringUUID; return self; } @end


4. alloc配置一台iPad,在透過init產生出場設定
  • 透過#import "iPad.h"引用iPad類別
  • 在objective中,建立物件是分為二階段的,首先透過alloc實際配置物件(也就是記憶體中),在透過init進行物件的初始化
  • 因此我們先透過alloc配置一台iPad,接著透過init初始化iPad物件,也完成iPad出廠值的UDID設定
#import <Foundation/Foundation.h> #import "iPad.h" int main(int argc, const char * argv[]) { @autoreleasepool { iPad * pad1 = [[iPad alloc] init]; NSLog(@"%@", pad1->UDID); iPad * pad2 = [[iPad alloc] init]; NSLog(@"%@", pad2->UDID); iPad * pad3 = [[iPad alloc] init]; NSLog(@"%@", pad3->UDID); } return 0; }



檔案連結:objective_c_class_alloc_init.zip



繼承下的正確建構:
其實objective c是物件導向的語言,因此也會有繼承的觀念,所以正確的init初始式的改寫,會先透過super關鍵字(表示父物件的關鍵字)進行init初始化,若建立成功則會在進行自身的初始化,如下:
- (id)init { self = [super init]; if (self != nil) { //透過NSUUID隨機產生代碼 NSUUID *UUID = [NSUUID UUID]; NSString* stringUUID = [UUID UUIDString]; //並設定為iPAD的UDID self->UDID = stringUUID; return self; } return nil; }


有參數的init:
若想在初始化的同時設定物件的其他資料值,這時可自訂初使化方法,如下:
iPad.h
#import <Foundation/Foundation.h> @interface iPad : NSObject { @public NSString * UDID; NSString * Type; NSInteger Capacity; } //定義一個自訂的初始式,其中定義type與capacity參數,以接收對應資料 - (id)initWithType:(NSString*)type andCapacity:(NSInteger)capacity; @end

iPad.m
#import "iPad.h" @implementation iPad - (id)init { self = [super init]; if (self != nil) { //透過NSUUID隨機產生代碼 NSUUID *UUID = [NSUUID UUID]; NSString* stringUUID = [UUID UUIDString]; //並設定為iPAD的UDID self->UDID = stringUUID; return self; } return nil; } - (id)initWithType:(NSString*)type andCapacity:(NSInteger) capacity { //透過init完全基本初始動作 self = [self init]; //將參數接收到的值帶入對應的實體變數中 self->Type = type; self->Capacity = capacity; return self; } @end

main.m
#import "iPad.h" #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { //呼叫initWithType:andCapacity:建立iPad,同時帶入Type與Capacity相關資料的值 iPad * pad1 = [[iPad alloc] initWithType:@"iPad" andCapacity:16]; iPad * pad2 = [[iPad alloc] initWithType:@"iPad 2" andCapacity:16]; iPad * pad3 = [[iPad alloc] initWithType:@"iPad Air" andCapacity:16]; } return 0; }