• <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>
            国内精品久久久久久久coent| 人妻无码αv中文字幕久久| 久久AⅤ人妻少妇嫩草影院| 国产精品99久久久久久猫咪| 久久男人中文字幕资源站| 亚洲va国产va天堂va久久| 九九久久99综合一区二区| 伊人 久久 精品| 97久久精品人人做人人爽| 久久九九兔免费精品6| 久久久久97国产精华液好用吗| 久久精品亚洲AV久久久无码| 国产精品久久久久久久久久免费| 国内精品久久国产| 久久成人18免费网站| 久久精品国产亚洲77777| 久久久久国产一区二区| 久久天天躁狠狠躁夜夜网站| 久久综合九色综合久99| 国产精品欧美久久久天天影视| 久久久国产视频| 看全色黄大色大片免费久久久| 久久成人影院精品777| 久久天天躁狠狠躁夜夜网站| 无码人妻久久一区二区三区蜜桃| 嫩草影院久久99| 久久免费精品一区二区| 久久人人妻人人爽人人爽| 久久AV高潮AV无码AV| 久久精品国产亚洲av瑜伽| 99久久精品九九亚洲精品| 99久久精品毛片免费播放| 国产亚洲欧美精品久久久| 久久精品中文騷妇女内射| 久久久一本精品99久久精品66 | 一本一道久久综合狠狠老| 蜜臀久久99精品久久久久久| 久久精品国产一区二区| 久久国产香蕉一区精品| 色婷婷噜噜久久国产精品12p | 亚洲人成伊人成综合网久久久|