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/14

[Objective-C] 一個空間,一把鑰匙 - NSDictionary & NSMutableDictionary


idea...



這台iPad是Kent的,這台iPad2是Gina的,這台iPad mini是Eric的,雖然我可以用NSArray將它們保存在一起,但我要怎麼區分這是誰的iPad呢?
我是類別NSDictionary,
沒問題,我除了可以幫您保存裝置外,
你還可以指定每個位置所使用的key,
如此你就可以依照key找到對應人員的裝置了。







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


2. 建立NSDictionary,以value&key的方式存入iPad物件
  • 建立NSDictionary,透過initWithObjectsAndKeys:以value&key的方式,將每台iPad以對應姓名的方式儲存
#import <Foundation/Foundation.h> #import "iPad.h" #import "iPad2.h" #import "iPadmini.h" int main(int argc, const char * argv[]) { @autoreleasepool { iPad * kentPad = [[iPad alloc] init]; iPad2 * ginaPad = [[iPad2 alloc] init]; iPadmini * ericPad = [[iPadmini alloc] init]; //使用dictionary, //讓ipad保存時可以透過key來對應到value值, //也就是可以將ipad value值對應到每一個擁有者的key, //initWithObjectsAndKeys:會以value,key,value,key的方式配對, //先value值,後對應key, //在此即將存在kentPad的pad物件valuevalue值,對應到"kent"這個key, //在此即將存在ginaPad的pad物件值,對應到"gina"這個key, //在此即將存在ericPad的pad物件value值,對應到"eric"這個key NSDictionary * dic = [[NSDictionary alloc] initWithObjectsAndKeys: kentPad, @"kent", ginaPad, @"gina", ericPad, @"eric", nil]; } return 0; }


3. objectForKey:給予key,以取出對應裝置
  • 透過objectForKey:訊息取得對應的裝置物件
  • 而NSDictionary皆以id型別的方式保存value與key,因此透過objectForKey:訊息取得的也是id型別的物件
#import <Foundation/Foundation.h> #import "iPad.h" #import "iPad2.h" #import "iPadmini.h" int main(int argc, const char * argv[]) { @autoreleasepool { iPad * kentPad = [[iPad alloc] init]; iPad2 * ginaPad = [[iPad2 alloc] init]; iPadmini * ericPad = [[iPadmini alloc] init]; //使用dictionary, //讓ipad保存時可以透過key來對應到value值, //也就是可以將ipad value值對應到每一個擁有者的key, //initWithObjectsAndKeys:會以value,key,value,key的方式配對, //先value值,後對應key, //在此即將存在kentPad的pad物件valuevalue值,對應到"kent"這個key, //在此即將存在ginaPad的pad物件值,對應到"gina"這個key, //在此即將存在ericPad的pad物件value值,對應到"eric"這個key NSDictionary * dic = [[NSDictionary alloc] initWithObjectsAndKeys: kentPad, @"kent", ginaPad, @"gina", ericPad, @"eric", nil]; //當然取出物件時, //即可透過objectForKey:訊息, //給予對應的key,取得對應的值, //如代入"kent"的key,則會取回kent的ipad //當然,NSDictionary也是以id的方式保存id與value, //因此取出時也會是id型別的物件 id kentDevice = [dic objectForKey:@"kent"]; id ginaDevice = [dic objectForKey:@"gina"]; id ericDevice = [dic objectForKey:@"eric"]; } return 0; }

檔案連結:objective_c_nsdictionary.zip



更多的NSDictionary使用方式:
NSMutableDictionary動態配對value&key存放的物件:
  • NSDictionary在透過initWithObjectsAndKeys:存入物件後即不可變更其中的內容
  • 而NSMutableDictionary則可動態變數其中的物件,透過setValue:forKey:與removeObjectForKey:訊息動態的增減保存的物件
#import <Foundation/Foundation.h> #import "iPad.h" #import "iPad2.h" #import "iPadmini.h" int main(int argc, const char * argv[]) { @autoreleasepool { iPad * kentPad = [[iPad alloc] init]; iPad2 * ginaPad = [[iPad2 alloc] init]; iPadmini * ericPad = [[iPadmini alloc] init]; //使用NSMutableDictionary //在此即將存在ericPad的pad物件value值,對應到"eric"這個key NSMutableDictionary * dic = [[NSMutableDictionary alloc] initWithObjectsAndKeys: kentPad, @"kent", nil]; //透過setValue:forKey:訊息動態加入新的value&key [dic setValue:ginaPad forKey:@"gina"]; [dic setValue:ericPad forKey:@"eric"]; //透過removeObjectForKey:動態移除特定key所對應的value&key [dic removeObjectForKey:@"kent"]; } return 0; }

檔案連結:objective_c_nsmutabledictionary.zip