• <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 閱讀(7729) 評論(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>
            精品久久久久久亚洲| 国产成人无码精品久久久久免费| 久久综合九色欧美综合狠狠| 久久久受www免费人成| 2020国产成人久久精品| 一本色道久久HEZYO无码| 久久精品国产半推半就| 内射无码专区久久亚洲| 婷婷综合久久中文字幕蜜桃三电影| 2022年国产精品久久久久| 久久综合九色欧美综合狠狠 | av无码久久久久不卡免费网站 | 精品多毛少妇人妻AV免费久久| 久久久久久久久66精品片| 国产精品99久久不卡| 久久久国产精华液| 久久播电影网| 9999国产精品欧美久久久久久| 久久久久久久精品妇女99| 久久久久亚洲AV无码专区网站| www性久久久com| 久久久久久亚洲精品成人| 久久受www免费人成_看片中文| 久久精品免费网站网| 91久久婷婷国产综合精品青草| 午夜久久久久久禁播电影| 久久中文字幕人妻熟av女| 久久久艹| 亚洲精品国精品久久99热| 久久久久久久国产免费看| 国产成人精品久久亚洲高清不卡 | 国产成人精品免费久久久久| 亚洲精品乱码久久久久久| 日韩中文久久| 久久精品国产亚洲AV影院| 久久人妻AV中文字幕| 无码人妻久久一区二区三区蜜桃| 2021最新久久久视精品爱| 国产亚洲精久久久久久无码77777 国产亚洲精品久久久久秋霞 | 精品99久久aaa一级毛片| 久久久久国产一级毛片高清版|