Save array of array to a file [ios] [duplicate] - ios

This question already has answers here:
objects conforming to nscoding will not writetofile
(2 answers)
Closed 8 years ago.
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?

you should init your NSMutableArray *storage, try:
NSMutableArray *storage = [[NSMutableArray alloc] init];

Related

Access and change values of an NSMutableDictionary from one class to another iOS

I am dealing with NSMutableDictionary in one class, But I want to change it's value in an another class.
Here is the code where it is declared.
#interface MyClass0 : NSObject
{
#public
NSMutableDictionary *valuee;
}
#property (nonatomic, copy) NSMutableDictionary *valuee;
#end
and in the implementation of myClass0 I do
#synthesize valuee;
and I also declare value as
valuee = #{#"name" : #"Aryan"};
Now I want to access and change the value of this dictionary in an another class.
Use #property (nonatomic, strong) NSMutableDictionary *valuee;
Now assign the value as
self.valuee = [[NSMutableDictionary alloc] init];
You can do this in the init method of your MyClass0.
Now you can access the value from another class like
MyClass0 *myClassInstance = [[MyClass0 alloc] init];
NSMutableDictionary *dict = myClassInstance.valuee;
You can write the getter by hand --
- valuee {
return valuee;
}
so the code instance.valuee will give you the original object not the copy.

Remove Duplicate Objects From NSMutableArray object property? [duplicate]

This question already has answers here:
How to remove all objects with the same property value but one in NSMutableArray
(3 answers)
Closed 9 years ago.
I have one NSObject with properties as following
#property (nonatomic, retain) NSString * destinationid;
#property (nonatomic, retain) NSString * destinationname;
#property (nonatomic, retain) NSString * assetid;
#property (nonatomic, retain) NSString * assetname;
#property (nonatomic, retain) NSString * assetdescription;
Here, I save this in NSMutableArray.
The data which I get from server contains same DestinationName, but different other properties.
I want to check, if same name of Object is already added to NSMutableArray, don't add it again.
I tried to user innner loops but no use :(
Thanks
Here's one solution:
NSArray *originalArray = ... // original array of objects with duplicates
NSMutableArray *uniqueArray = [NSMutableArray array];
NSMutableSet *names = [NSMutableSet set];
for (id obj in originalArray) {
NSString *destinationName = [obj destinationname];
if (![names containsObject:destinationName]) {
[uniqueArray addObject:obj];
[names addObject:destinationName];
}
}
One way to do it is to create a new array that only contains the destination names from your other array, and check if the name from your server is contained there:
NSArray *names = [self.mut valueForKey:#"destinationname"];
if (![names containsObject:destName]) {
[self.mut addObject:newObjectFromServer];
}
mut is the name of my mutable array, and destName is the destination name in the object your getting from the server.
This function should do the job:
BOOL contains(id object, NSMutableArray *array)
{
for (id array in yourArray)
if ([object.destinationname isEqualToString:yourObject.destinationname])
return YES;
return NO;
}
Usage example:
if (!contains(object, array))
[array addObject:object];

Accessing NSString from another .m file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I need to access userEMail (NSString) property in Class2.m but its returning null.
Class1.h:
#interface AuthController : UIViewController
{
#public NSString *const userPassword;
#public NSString *const userEMail;
#public NSString *const userFullName;
}
#property (nonatomic, strong) NSString *userEMail;
Then in Class1.m im saving userEMail.
Class2.m:
AuthController *ac = [[AuthController alloc] init];
NSLog(#"%#", ac.userEMail);
I just put basic step here, change it as per your requirement.
You just need to write
#property (nonatomic, strong) NSString *userEMail;
in your first.h file
write second.h file
#property (nonatomic, strong) NSString *gotUserEMail;
And your first.m file
you have gotUserEMail string with some value..
pass it to anotherViewController such liek,
secondViewController *addView = [[secondViewController alloc] init];
addView.gotUserEMail = userEMail;
.
.
.
.
EDITE
Okay then you need to use NSUserDefault.
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setValue:userEMail forKey:#"myEmailString"];
[userDefaults synchronize];
get anywhere in your project by,
NSUserDefaults *data = [NSUserDefaults standardUserDefaults];
NSString *gotEmail = [data floatForKey:#"myEmailString"];
After reading a comment on another answer
I need to get userEMail value in multiple classes, so its wrong to send data to every class
It sounds more like you are after a singleton class. So try something like this
AuthObject.h
#interface AuthObject : NSObject
#property (nonatomic, strong) NSString *userEMail;
+ (AuthController*)authInstance;
#end
AuthObject.m
#import "AuthObject.h"
#implementation AuthObject
#synthesize userEMail = _userEMail;
static AuthObject *authObjectSharedInstance = nil;
+ (AuthObject *)authInstance
{
static dispatch_once_t instanceToken;
dispatch_once(&instanceToken, ^{
authObjectSharedInstance = [[AuthObject alloc] init];
});
return authObjectSharedInstance;
}
#end
then in another method in another class as long as you have imported AuthObject.h somewhere you can do
- (void)someMethodIMadeUpInClass1
{
AuthObject *authObj = [AuthObject authInstance];
[authObj setUserEMail:#"myemail#address.com"];
}
then in a completely different class you can do
- (void)someMethodIMadeUpInClass2
{
AuthObject *authObj = [AuthObject authInstance];
NSLog(#"my email : %#", [authObj userEMail];
}
In theory if you're creating a singleton class and not going through a sharedInstance (authInstance here) then that code is broken. Attempting to hide that brokenness is just going to cause pain later on. This is why I would chose a singleton class over using NSUserDefaults.

iOS: NSMutableDictionary propery won't set [duplicate]

This question already has answers here:
Having trouble adding objects to NSMutableArray in Objective C
(2 answers)
Closed 8 years ago.
For some reason I can't get an array added to a nsmutabledictionary. I have it declared as a property in my .h file here:
#interface TCMExhibitFeedStore : NSObject
#property (nonatomic, strong) NSMutableDictionary *allLevels;
#property (nonatomic, strong) NSMutableDictionary *storedLevels;
+ (TCMExhibitFeedStore *)sharedStore;
- (void)fetchExhibtFeedWithCompletion:(void (^)(NSMutableDictionary *obj, NSError *err))block;
- (TCMLevel *)createLevel:(TCMLevelRemote *)l;
- (TCMExhibit *)createExhibit:(TCMExhibitRemote *)e;
- (BOOL)saveChanges;
#end
Then, I'm trying to add an empty array to it in a function in the .m file like this
[_storedLevels setObject:[[NSMutableArray alloc] init] forKey:#"levels"];
However, when I step through the application this never gets added. Looking in the debugger shows it as
_storedLevels NSMutableDictionary * 0x00000000
NSMutableDictionaries are nothing new to me. I'm just going a bit crazy trying to find my error because this all looks normal to me.
The following lines confuses me...
[_storedLevels setObject:[[NSMutableArray alloc] init] forKey:#"levels"];
Instead of above Use:
self.storedLevels = [[NSMutableDictionary alloc] init];
[self.storedLevels setObject:... forKey:#"levels"];
NOTE: Whenever you see 0x00000000
This means your object is not alloc-ated.
Therefore you need to alloc+init them, before setting any array/value to them.
0x00000000 means nil. All properties and instance variables of Objective C objects are initialized to nil. Hence, we can tell that you have never initialized your properties.
You can to initialize the dictionaries in the designated initializer for TCMExhibitFeedStore. (Or before you access them to add elements)
self.storedLevels = [NSMutableDictionary dictionary];
self.allLevels = [NSMutableDictionary dictionary];

Subtract an array of complex objects from another array of same kind of objects ios

I know that subtracting an NSArray from NSArray if it is a single basic object found here
But what i have is an object like this
#interface Set : NSObject
#property (nonatomic, strong) NSString *ItemId;
#property (nonatomic, strong) NSString *time;
#property (nonatomic, strong) NSString *Category_id;
#property (nonatomic, strong) NSString *List_id;
#property (nonatomic, strong) NSString *name;
#end
How can i delete an array having the set object from another array with the same?
It can be done by iterations that i knw.Is there some other way ?
EDIT: For clarity
I have Array A with 5 Set objects and I have 4 Set Objects in Array B
array A and Array B contain 3 set objects with common values..[Note : memory may be different] common
All I need is an Array C =Array A - Array B that has 2 objects in resulting array C
Thank You :)
You need to implement the - (NSUInteger)hash and - (BOOL)isEqual:(id)object method in Set class.
For eg:-
- (NSUInteger)hash {
return [self.ItemId hash];
}
- (BOOL)isEqual:(id)object
{
return ([object isKindOfClass:[self class]] &&
[[object ItemId] isEqual:_ItemId])
}
After that try this:
NSMutableSet *set1 = [NSMutableSet setWithArray:array1];
NSMutableSet *set2 = [NSMutableSet setWithArray:array2];
[set1 intersectSet:set2]; //this will give you only the obejcts that are in both sets
NSArray *commonItems = [set1 allObjects];
[mutableArray1 removeObjectsInArray:commonItems];//mutableArray1 is the mutable copy of array1
mutableArray1 will have all objects in the same order as earlier after removing common objects.
By using NSSet and NSPredicate we can meet your requirement.
Assessors *ass1 = [[Assessors alloc] init];
ass1.AssessorID = #"3";
Assessors *ass2 = [[Assessors alloc] init];
ass2.AssessorID = #"2";
Assessors *ass3 = [[Assessors alloc] init];
ass3.AssessorID = #"1";
Assessors *ass4 = [[Assessors alloc] init];
ass4.AssessorID = #"2";
NSSet *nsset1 = [NSSet setWithObjects:ass1, ass2, nil];
NSSet *nsset2 = [NSSet setWithObjects:ass3, ass4, nil];
// retrieve the IDs of the objects in nsset2
NSSet *nsset2_ids = [nsset2 valueForKey:#"AssessorID"];
// only keep the objects of nsset1 whose 'id' are not in nsset2_ids
NSSet *nsset1_minus_nsset2 = [nsset1 filteredSetUsingPredicate:[NSPredicate predicateWithFormat:#"NOT AssessorID IN %#",nsset2_ids]];
for(Assessors *a in nsset1_minus_nsset2)
NSLog(#"Unique ID : %#",a.AssessorID);
Here Assessors is my NSObject Class (Set in your case) and AssessorID is one property of that class.
Hope this can help.

Resources