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/03/13

[Objective-C] 這裡什麼都沒有!!! - nil / Nil / NULL


idea...



等等,這個變數容器已經不在存放著iPad了,但要怎麼從變數容器中移除呢?
我是nil,
你也可以叫我Nil或是NULL,
我代表"什麼都沒有",
只要你將我放在變數容器中就可以清除iPad於存在了。



1. 建立iPad
  • 建立iPad類別
iPad.h
#import <Foundation/Foundation.h> @interface iPad : NSObject -(void) connectInternetWithWifi; @end


2. 宣告變數存放iPad
  • 在此宣告變數以存放iPad,並放入iPad
#import <Foundation/Foundation.h> #import "iPad.h" int main(int argc, const char * argv[]) { @autoreleasepool { iPad * pad1 = [[iPad alloc]init]; iPad * pad2 = [[iPad alloc]init]; id pad3 = [[iPad alloc]init]; } return 0; }


3. 代入nil清除變數
  • 此時若想要讓變數清空,則可代入nil,以表示變數中沒有任何的東西了
  • 當然在Objective-C中,nil、Nil與NULL皆是相同的
#import <Foundation/Foundation.h> #import "iPad.h" int main(int argc, const char * argv[]) { @autoreleasepool { iPad * pad1 = [[iPad alloc]init]; iPad * pad2 = [[iPad alloc]init]; id pad3 = [[iPad alloc]init]; //透過nil/Nil/NULL將變數清空, //以達到移除變數中物件的目的 pad1 = nil; pad2 = Nil; pad3 = NULL; } return 0; }

檔案連結:objective_c_nil.zip



nil可以接放所有訊息,乎略所有訊 息:
  • 還記得當我們傳送一個訊息給物件時,若物件無對應方法執行的話程式將會中止
  • 但若是為nil、Nil或是NULL,則會乎略所有訊息,而不會造成程式當機中止,這是一個很好的特性(個人非常覺得),程式最多不執行您預期的功能,但至少不會造成當機的困擾,如下列程式:
#import <Foundation/Foundation.h> #import "iPad.h" int main(int argc, const char * argv[]) { @autoreleasepool { iPad * pad1 = [[iPad alloc]init]; //透過nil/Nil/NULL將變數清空, //以達到移除變數中物件的目的 pad1 = nil; //這時傳送連線網路的connectInternetWithWifi訊息, //程式也不會當機中斷 [pad1 connectInternetWithWifi]; } return 0; }

ARC下,nil後自動釋放物件:
  • ARC,全名Automatic Reference Counting,是objective-c用來進行記憶體管理的手法,在iOS 5.0版時引進
  • 過網的記憶體管理是需要透過計數的方式,有人使用就+1,沒有使用就-1,直到此物件參照的次數為0時,這個物件就會被釋放掉
  • 但在ARC你只需簡單的將變數設為nil、Nil或NULL即可,編譯器會幫您自動加上計數相關的程式,所以計憶體管理最重要的一件事,就是將不要用的物件,其存放它的變數設定為nil,如此就可完全作好記憶體管理,當然,重複的給予nil也是可以的