Write in Main Bundle directory. Is it allowed?

后端 未结 2 633
轮回少年
轮回少年 2021-01-13 13:51

I was pretty sure that writing in the main Bundle isn\'t possible in iOS ... for example an operation like :

NSString *path = [[NSBundle mainBundle] pathFor         


        
2条回答
  •  南笙
    南笙 (楼主)
    2021-01-13 14:45

    That's not a link to the main bundle. That's a path to the resources folder, and a plist within that folder.

    Hence the function name pathForResource...

    Everything in the main bundle is cryptographically signed when you compile the app. The resources folder isn't though. You can write to and from that freely.

    for @jrturton

    // we need to get the plist data...
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Symptoms" ofType:@"plist"];
        NSMutableArray *dataArray = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
    
        // add a new entry
        NSDictionary *addQuestion = [[NSDictionary alloc] initWithObjectsAndKeys:@"Blank.png",@"Icon",
                                     [NSString stringWithFormat:@"%i",r],@"ID",
                                     [titleTextField text],@"Title",
                                     [questionTextField text],@"Text",
                                     qnType,@"Type",
                                     @"1",@"Custom",
                                     [NSArray arrayWithObjects:@"Yes",@"No",nil],@"Values",
                                     [unitsTextField text],@"Units",
                                     nil];
        [dataArray addObject:addQuestion];
        [addQuestion release];
    
        // rewrite the plist
        [dataArray writeToFile:plistPath atomically:YES];
    

提交回复
热议问题