How to wrap a Struct into NSObject

Hm, try to look at the NSValue at https://developer.apple.com/documentation/foundation/nsvalue You can use it like struct aStruct { int a; int b; }; typedef struct aStruct aStruct; Then sort of “wrap” it to an NSValue object like: aStruct struct; struct.a = 0; struct.b = 0; NSValue *anObj = [NSValue value:&struct withObjCType:@encode(aStruct)]; NSArray *array = @[anObj]; To … Read more

iOS: store two NSMutableArray in a .plist file

Create an NSArray containing your two NSMutableArrays. NSArray *array = [NSArray arrayWithObjects:<#(id), …#>, nil]; Write the array to a file. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); NSString *libraryDirectory = [paths objectAtIndex:0]; NSString *location = [libraryDirectory stringByAppendingString:@”/somefilename.plist”]; [array writeToFile:location atomically:YES]; Load the array from the file. NSString *path = [bundle pathForResource:@”file” ofType:@”plist”]; NSArry *array = (path … Read more

insert object in an NSMutableArray saved with NSUserDefaults

NSUserDefaults always returns immutable objects, even if the original object was mutable. It’s in the documentation for objectForKey: The returned object is immutable, even if the value you originally set was mutable. You will need to create a copy of the returned object before you modify it, using [NSMutableArray arrayWithArray:] Probably also best to use … Read more

Sort NSArray of custom objects based on sorting of another NSArray of strings

Hereby, I compare directly the index of obj1.assetID in stringOrder with the index of obj2.assetID in stringOrder (using Objective-C literals for @() to transform NSString => NSNumber) [items sortUsingComparator:^NSComparisonResult(Attribute *obj1, Attribute *obj2) { return [@([stringOrder indexOfObject:obj1.assetID]) compare:@([stringOrder indexOfObject:obj2.assetID])] }]; Or without ObjC literals : [items sortUsingComparator:^NSComparisonResult(Attribute *obj1, Attribute *obj2) { return [[NSNumber numberWithInt:[stringOrder indexOfObject:obj1.assetID]] compare:[NSNumber … Read more

is it possible to save NSMutableArray or NSDictionary data as file in iOS?

To write in file NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *filePath = [documentsDirectory stringByAppendingPathComponent:FILE_NAME]; [myArray writeToFile:filePath atomically:YES]; To read from file NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *filePath = [documentsDirectory stringByAppendingPathComponent:FILE_NAME]; myArray = [NSArray arrayWithContentsOfFile:filePath]; myArray will be nil if file is not … Read more

How to save NSMutablearray in NSUserDefaults

Note: NSUserDefaults will always return an immutable version of the object you pass in. To store the information: // Get the standardUserDefaults object, store your UITableView data array against a key, synchronize the defaults NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; [userDefaults setObject:arrayOfImage forKey:@”tableViewDataImage”]; [userDefaults setObject:arrayOfText forKey:@”tableViewDataText”]; [userDefaults synchronize]; To retrieve the information: NSUserDefaults *userDefaults = [NSUserDefaults … Read more