iOS development encounters the five pits--solve the project already exists plist table, the data cannot deposit the question

Source: Internet
Author: User

Want to write this blog in fact, a two months ago when the development of the meeting to want to put this issue as a blog, but I have been lazy and have no time, the matter to delay, fortunately at that time know that they must be the problem to describe it, lest other people encounter this problem will be entangled for a long time (actually I, Basic knowledge, so I took the process to the record.

To this blog name, is not know how to name the word (language is not good), because it is difficult to describe clearly, so the iOS development encountered in the pit series of articles (if you crossing think this is really my basic problem, please tell Owen, I will change over, by the way also learn)

This is probably the case:


You want to give your app a built-in plist table, so that when the app initializes the data directly from the inside read out to load, it is common that the United States and the client has a local area plist, because this if every time from the server to obtain, because it is larger, So the download time is relatively long, to the user's experience is very bad, so simply built-in!

But the problem is, if you want to write this plist table into the data, well, then congratulations into the pit, because you can not write in any way! (can only be read and not written in the project)

Here's why: the plist table you added directly under the project directory is not the same as what we usually call the document directory.

As you can see from the code:

1  NSString *filepatch = [[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) Objectatindex:0]stringbyappendingpathcomponent:@ "ads.plist"];

Where to display in the folder:

/users/wayneliu_mac/library/developer/coresimulator/devices/e6c97a37-a9c1-4f4a-a3ea-efbb75c1bb43/data/ Containers/data/application/5e03dc76-7326-4e24-bdbe-f9d5d3072899/documents/ads.plist

and

1 nsstring *plistpath = [[NSBundle mainbundle] Pathforresource:@ "Ads" ofType:  @ "plist"]

Where to display in the folder:

/users/wayneliu_mac/library/developer/coresimulator/devices/e6c97a37-a9c1-4f4a-a3ea-efbb75c1bb43/data/ Containers/bundle/application

They've been branching out here!

The stored data is only three folders of documentIt has to be written in the document above. If the direct bundle is not authorized, itunes can be seenOK, here's the solution: to save the data inside, you can only operate in the document under the sandbox, so you must first copy the existing plist form into the document in your project.
1- (void) createeditablecopyofplistifneeded{2Nsfilemanager *filemanager =[Nsfilemanager Defaultmanager];3NSString *filepatch =Adsplistpath;4BOOL Exists =[FileManager Fileexistsatpath:filepatch];5 //    //Delete the data inside the real machine6 //BOOL success1 = [FileManager removeitematpath:filepatch error:nil];7 //NSLog (@ "%HHD", success1);8     if(!Exists) {9 //nsstring *plistpath = [[NSBundle mainbundle] pathforresource:@ "Ads" oftype:@ "plist"];TenNSString *plistpath = [[[NSBundle Mainbundle]resourcepath]stringbyappendingpathcomponent:@"ads.plist"]; OneNserror *error; ABOOL success = [FileManager Copyitematpath:plistpath topath:filepatch error:&ERROR]; -         if(!success) { -NSAssert1 (0,@"Error writing to file: '%@ '.", [Error localizeddescription]); the         } -     } -  -Nsmutablearray *data =[[Nsmutablearray alloc] initwithcontentsoffile:filepatch]; +      -      +     if(Data.count = =0){ ANSLog (@"==plist No data"); at}Else{ -Self.adsarr =data; -NSLog (@"plist Data%@", data);//print data directly.  -         //[Data removeallobjects]; -         //[Data Writetofile:filepatch atomically:yes]; -     } in}

and then use
1  NSString *filepatch = [[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) Objectatindex:0]stringbyappendingpathcomponent:@ "ads.plist"];

This code gets the document directory, and then you can write to it!

iOS development encounters the five pits--solve the project already exists plist table, the data cannot deposit the question

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.