Can you save the position of a node in an sks file - xcode

I'm trying to create a SKScene file where we create empty nodes at the places where our players and other objects will be placed. The problem is that the positions all have to be set in code. Is it possible to set the position in the sks file instead?
I did notice there is a Position label in the node inspector, but that doesn't appear to have any effect.

Write data with NSUserDefaults:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *nodeDict = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithFloat:myNode.position.x], #"posX",
[NSNumber numberWithFloat:myNode.position.y], #"posY",
nil];
[defaults setObject: nodeDict forKey:#"nodeDict"];
[defaults synchronize];
Read data with NSUserDefaults:
NSDictionary * nodeDict = [[NSUserDefaults standardUserDefaults] objectForKey:#"nodeDict"];
float posX = [[nodeDict objectForKey:#"posX"] floatValue];
float posY = [[nodeDict objectForKey:#"posY"] floatValue];
Obviously you can write a lot more data than the example above. If you have several nodes, create a dictionary for each one and store them all in an array. Write the array to NSUserDefaults.
Keep in mind that you can only store objects in a dictionary and not primitives like int, float, etc... That is why you have to convert them into NSNumber.

Related

NSUserDefaults: how to get only the keys I've set

All of the methods to get keys from NSUserDefaults return heaps of keys from domains other than the app itself (e.g., NSGlobalDomain). I just want the keys and values that my app has set. This is useful for debugging and verifying that there are no orphaned keys, etc.
I could ignore the keys that aren't mine (if I know all of them -- during development I may have set keys I'm no longer using), but there might be a collision of keys in other domains and I'll not see my app's value.
Other discussions suggest looking at the dictionary file associated with the app, but that's not very elegant.
How can I get only my app's keys form NSUserdefaults?
Elegant approach
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
NSDictionary *dict = [defaults persistentDomainForName:bundleIdentifier];
File approach:
NSString *bundleIdentifier = [[NSBundle mainBundle] objectForInfoDictionaryKey:#"CFBundleIdentifier"];
NSString *path = [NSString stringWithFormat:#"~/Library/Preferences/%#.plist",bundleIdentifier];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:[path stringByExpandingTildeInPath]];
NSArray *keys = [dict allKeys];
Tested with sandboxing.
Marek's code updated to Swift 5:
guard let bundleIdentifier = Bundle.main.bundleIdentifier else { return }
let dict = UserDefaults.standard.persistentDomain(forName: bundleIdentifier)

How to save integer from Singleton in an NSUserDefault to use after app is relaunched in Xcode 5?

I have a Singleton that keeps track of my "score" over all my ViewControllers, and that works great. But I want to save my "score" from my Singleton in an NSUserDefault so I can use it after the app has been relaunched. My state preservation works for everything, but keeping the current score. My Singleton looses its data when the app is closed and I understand NSUserDefault saves the data between launches. I save my "score" from my Singleton to "integer" in my NSUserDefault. I then try to display an image that is named 0.png, 1.png, 2.png and so on until 10.png. But this image is displayed as 0.png every time.
(I really hope my question isn't stupid. I have read tutorials and tried different things for a week, and this is my first question here.)
Please show me where my code goes wrong.
viewcontroller.m
//Save Singleton score to NSUserDefault integer
[super viewDidLoad];
optionsshared = [Singleton sharedSingletonManager];
integer = optionsshared.score;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:integer forKey:#"integer"];
[defaults synchronize];
// Set Image
ScoreImg.image = [UIImage imageNamed:[NSString stringWithFormat:#"%d.png", integer]];
This code works:
//Set Singleton to number from NSUserDefault in case app was terminated
optionsshared = [Singleton sharedSingletonManager];
NSUserDefaults *getObj = [NSUserDefaults standardUserDefaults];
int Number = [((NSNumber*)[getObj objectForKey:#"stars"])intValue];
optionsshared.score = Number;
[super viewDidLoad];
//Set image from number in NSUserDefaults
ScoreImg.image = [UIImage imageNamed:[NSString stringWithFormat:#"%d.png", Number]];
}

NSUserDefaults and registerDefault, a standard behavior?

I need to store a numeric parameter in NSUserDefault, this parameter must be as default 1
So i write this code :
NSNumber *one = [NSNumber numberWithInt:100];
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
one,#"my-param",
nil];
[def registerDefaults:dict];
This parameter has a bind over a checkbox in IB so i can check that it's correctly set and when i start my app i see checkbox with state "on".
On the other side i must check this value programmatically so... i do something like :
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSInteger my_param = [defaults integerForKey:#"my-param"];
I excepted that since the value is not set by the user this function return value that i set as default (in my case "1") but with my surprise i found that if value has never been set by user it returns 0 ... as you can understand this's terrible :P because now i can't understand if this "0" is obtained by user choice of this's a consequence of a non-set value... how can i write code to manage this situation ?
To store the numeric values in NSUserDefaults you can simply directly use as follows:
To set the integer Value in NSUserDefault use as follows below:
NSInteger lInteger = 10;
[[NSUserDefaults standardUserDefaults] setInteger:lInteger forKey:#"integerkey"];
[[NSUserDefaults standardUserDefaults] synchronize];
And to use that NSInteger value anywhere in your project use as below:
NSInteger linteger = [[NSUserDefaults standardUserDefaults] integerForKey:#"integerkey"];
And i didnt understand your question exactly, but anyhow i think this above code may help you.

Sort a NSMutuableArray

I have a NSMutableArray that is loaded with a inforamtion from a dictionary...
[self.data removeAllObjects];
NSMutableDictionary *rows = [[NSMutableDictionary alloc] initWithDictionary:[acacheDB.myDataset getRowsForTable:#"sites"]];
self.data = [[NSMutableArray alloc] initWithArray:[rows allValues]];
There are two key value pairs in the rows dictionary.
I need to sort the self.data NSMutableArray in alphabetical order.
How is this accomplished??
thanks
tony
If the values are plain strings you can use the following to create a sorted array:
NSArray *sorted = [values sortedArrayUsingSelector:#selector(caseInsensitiveCompare:)];
This should do:
[self.data removeAllObjects];
NSArray *values = [[acacheDB.myDataset getRowsForTable:#"sites"] allValues];
NSSortDescriptor *alphaDescriptor = [[NSSortDescriptor alloc] initWithKey:#"DCFProgramName" ascending:YES selector:#selector(localizedCaseInsensitiveCompare:)];
NSArray *sortedValues = [values sortedArrayUsingDescriptors:[NSMutableArray arrayWithObjects:alphaDescriptor, nil]];
[alphaDesc release];
[self.data addObjectsFromArray:sortedValues];
There's no need to clear an NSMutableArray if you're replacing it shortly afterwards.
There's no need to create an additional NSMutableDictionary, if you're not modifying anything in it.
There's no need to create an additional NSMutableArray, if you could just as well just add the values to the existing one.
Also: There are some serious memory leaks in your code. (2x alloc + 0x release = 2x leak)
Edit: updated code snippet to reflect OP's update on data structure.

Setting QTMovie attributes

I'm trying to create a QTVR movie via QTKit, and I've got all the frames in the movie. However, setting the attributes necessary doesn't seem to be having any effect. For example:
NSNumber *val = [NSNumber numberWithBool:YES];
[fMovie setAttribute:val forKey:QTMovieIsInteractiveAttribute];
val = [NSNumber numberWithBool:NO];
[fMovie setAttribute:val forKey:QTMovieIsLinearAttribute];
If I then get the value of these attributes, they come up as NO and YES, respectively. The movie is editable, so I can't understand what I'm doing wrong here. How can I ensure that the attributes will actually change?
What I do when I want to export a Quicktime movie is something like the following:
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], QTMovieExport,
[exportSettings objectForKey: #"subtype"], QTMovieExportType,
[exportSettings objectForKey: #"manufacturer"], QTMovieExportManufacturer,
[exportSettings objectForKey: #"settings"], QTMovieExportSettings,
nil];
BOOL didSucceed = [movie writeToFile: tmpFileName withAttributes:dictionary error: &error];
Those attributes are documented as things you can read but not write. However, you might be able to set them when you create the movie, with initWithAttributes:error:.

Resources