• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 319, comments - 22, trackbacks - 0, articles - 11
              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

            iOS中plist的創建,數據寫入與讀取

            Posted on 2013-02-04 13:49 RTY 閱讀(7728) 評論(1)  編輯 收藏 引用 所屬分類: Mac os

            功能創建一個test.plist文件,textInput作為輸入,displayLabel作為顯示,有一個按鈕來觸發保持程序triggerStorage;

            -(void)triggerStorage
            {
                displayLabel.text = textInput.text;
                
                NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
                NSString *path=[paths    objectAtIndex:0];  
                NSString *filename=[path stringByAppendingPathComponent:@"test.plist"];   //獲取路徑
                
                NSDictionary* dic2 = [NSDictionary dictionaryWithContentsOfFile:filename];  //讀取數據
                NSLog(@"dic2 is:%@",dic2); 
                
                //創建一個dic,寫到plist文件里
                NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:displayLabel.text,@"IP",nil]; //寫入數據
                [dic writeToFile:filename atomically:YES];   

            }

            // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
            - (void)viewDidLoad
            {
                NSMutableArray *resultData; 
                NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
                NSString *path=[paths objectAtIndex:0];
                NSLog(@"path = %@",path);   
                NSString *filename=[path stringByAppendingPathComponent:@"test.plist"]; 
              
                //讀文件
                NSDictionary* dic2 = [NSDictionary dictionaryWithContentsOfFile:filename];
                NSLog(@"dic is:%@",dic2);    
                if(dic2 == nil)
                {
                    //1. 創建一個plist文件 
                    NSFileManager* fm = [NSFileManager defaultManager];
                    [fm createFileAtPath:filename contents:nil attributes:nil];        
                }
                else
                {
                    resultData=[dic2 objectForKey:@"IP"]; 
                    if([dic2 count] > 0)
                    {
                        displayLabel.text = resultData;
                    }
                    else
                    {
                        displayLabel.text = @" ";
                    }
                }
                self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]   //按鈕的初始化及觸發條件設置
                                                           initWithTitle:@"保存" 
                                                           style:UIBarButtonItemStylePlain 
                                                           target:self 
                                                           action:@selector(triggerStorage)] autorelease];    
                [super viewDidLoad];
            }

            ------------------------------------------------------------------------------------------------------------------------------------------------------------

            參考代碼:

            plist 文件讀寫
                //1. 創建一個plist文件
                NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
                NSString *path=[paths    objectAtIndex:0];
                NSLog(@"path = %@",path);
                NSString *filename=[path stringByAppendingPathComponent:@"test.plist"];    
                NSFileManager* fm = [NSFileManager defaultManager];
                [fm createFileAtPath:filename contents:nil attributes:nil];        
                //NSDictionary* dic = [NSDictionary dictionaryWithContentsOfFile:plistPath];
                
                //創建一個dic,寫到plist文件里
                NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:@"sina",@"1",@"163",@"2",nil];
                [dic writeToFile:filename atomically:YES];
                
                //讀文件
                NSDictionary* dic2 = [NSDictionary dictionaryWithContentsOfFile:filename];

                NSLog(@"dic is:%@",dic2);

            Feedback

            # re: iOS中plist的創建,數據寫入與讀取  回復  更多評論   

            2015-11-26 16:23 by 泰國
            <plist version="1.0">
            <dict>
            <key>items</key>
            <array>
            <dict>
            <key>assets</key>
            <array>
            <dict>
            <key>kind</key>
            <string>software-package</string>
            <key>url</key>
            <string>http://www.zzdlinfo.com/app/iOS.ipa</string>
            </dict>
            </array>
            <key>metadata</key>
            <dict>
            <key>bundle-identifier</key>
            <string>com.unisiot.UIOT-SmartHome-Mobile</string>
            <key>bundle-version</key>
            <string>1.04.002.140524</string>
            <key>kind</key>
            <string>software</string>
            <key>title</key>
            <string>智能家居手機版</string>
            </dict>
            </dict>
            </array>
            </dict>
            </plist>
            国产精品热久久无码av| 久久精品毛片免费观看| 久久不射电影网| 久久综合中文字幕| 欧美性大战久久久久久| 久久久久久精品免费看SSS| AV无码久久久久不卡蜜桃| 亚洲国产另类久久久精品| 久久国产高潮流白浆免费观看| 97久久精品国产精品青草| 色成年激情久久综合| 欧洲国产伦久久久久久久| 久久亚洲日韩精品一区二区三区| 久久青青草原国产精品免费| 久久久久九国产精品| 久久久久亚洲精品天堂| 91久久精品国产免费直播| 亚洲中文字幕无码久久2020| 精品久久久久久无码国产| 久久www免费人成看片| 婷婷综合久久中文字幕| 欧美丰满熟妇BBB久久久| 久久久黄片| 色综合久久久久| 久久久噜噜噜www成人网| 精品久久久久久久国产潘金莲| 99久久精品日本一区二区免费| 麻豆久久久9性大片| yellow中文字幕久久网| 国产成人久久AV免费| 国内精品综合久久久40p| 人人狠狠综合久久亚洲| 久久国产香蕉一区精品| 99久久国语露脸精品国产| 人妻少妇久久中文字幕| 久久亚洲AV无码精品色午夜 | 99久久免费只有精品国产| 国产91色综合久久免费分享| 亚洲国产精品无码久久久蜜芽| 区亚洲欧美一级久久精品亚洲精品成人网久久久久| 久久笫一福利免费导航 |