Storing object on iOS - best solution?

前端 未结 2 1164
[愿得一人]
[愿得一人] 2021-01-21 07:21

I\'m building an app for a blog where a user can save their favorite posts.

When they do, I want to store my object which contains: the post\'s URL, title and image URL.

2条回答
  •  猫巷女王i
    2021-01-21 07:41

    Since favorites for articles generally wont be 100K item then I would use NSDictionary for an item and store them into a NSMutableArray and then save them to a file. It is simple to use and you can also export the favorites to a file or even iCloud to share between devices.

    NSMutableDictionary *item = [[ NSMutableDictionary alloc]init];
    [item setObject:@"www.google.com" forKey:@"url"];
    [item setObject:@"Google" forKey:@"title"];
    
    //Add each item to the Favourites array
    //You should declare this outside of the "addToFavourites" function.
    NSMutableArray *Favourites = [[NSMutableArray alloc]initWithObjects: nil];
    [Favourites addObject:item];
    
    //Save the Favourites NSMutableArray to the file.
    if([Favourites writeToFile:path atomically:NO]) 
       NSLog(@"Favourites are saved!");
    

提交回复
热议问题