-2

I would like to have an array of model and a field of this model is a mutable array.

Let's say, for example ,in my Model.h I have :

@interface myModel : NSObject

  @property (assign,nonatomic) NSString* name;

  @property (assign,nonatomic) NSString* time;

  @property (assign,nonatomic) NSMutableArray * songs;

@end

then my view controller I have:

NSMutableArray * Storage;

myModel * arr;

arr =[[alarmModel alloc] init];

arr.name=@"pippo";

arr.time=@"01:00:00";

arr.songs=[NSMutableArray arrayWithObjects:@"pippo",@"pluto",@"paperino", nil];

[storage addObject:arr];

[storage writeToFile:filePath atomically:YES]

the last command return "NO" it means that write to file failed. Is it possible to do what I want to do?

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Grisu70
  • 57
  • 5
  • shouldn't `arr = [[alarmModel alloc] init];` be `arr = [[myModel alloc] init];` ?? also what @Ilario suggested in his answer. (_basically you have only declared the object, you haven't initialized it_) – staticVoidMan Nov 27 '13 at 14:26
  • check @staticVoidMan's comment and.... in addition... "show" your filePath... – TonyMkenu Nov 27 '13 at 14:30
  • 1
    Dont you have to implement any NSCoding protocol, will this be able to save your model and load them back ? – Sandeep Nov 27 '13 at 14:57
  • You can write an array to a file only if it only contains property list types (`NSArray`, `NSDictionary`, `NSString`, `NSNumber`...). Your `myModel` class is not a property list type, so you should use `NSCoding` as mentioned in the previous comment, or use an `NSDictionary` instead of your `myModel` class. Also, `assign` is probably not the storage that you want for your properties in the `myModel` class (it either means your `songs` array is leaked (without ARC), or not retained at all (with ARC it's an error)). – Guillaume Nov 27 '13 at 15:26

1 Answers1

3

you should init your NSMutableArray *storage, try:

NSMutableArray *storage = [[NSMutableArray alloc] init];
Ilario
  • 5,979
  • 2
  • 32
  • 46