I have a problem.
I have an array which is called toolsViewValues and when I'm saving, I get an
"EXC_BAD_ACCESS" and that's all... :-(
Yet, the objects inside the array are not nil.
NSArray *toolsViewValues = [[NSArray alloc] initWithObjects:
preferedLanguage,
needsSwitch.isOn,
vocSlider.value,
exprSlider.value,
gramSlider.value,
nil];
Any idea ?
Thanks a lot.
Mike
You must make sure that everything you're putting into the array is an object. Use NSString, NSNumber and/or NSValue.
It looks like isOn returns a boolean (judging by naming convention). Booleans cannot be put into an NSArray.
You'll need to wrap it in an NSNumber using numberWithBool:.
In invite those who find it hard to understand to read the following litterature :
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/Reference/Reference.html
I hope that will help.
Thanks again.
Related
I'm using RestKit, and have yet to come across how to do a simple PUT of a body like this:
<parameters></parameters>
Can someone please give a code example?
This is kind of a confusing PUT since your query parameter is actually titled "parameters". Are you sure that was the intention? Try something like this.
NSDictionary* params = [NSDictionary dictionaryWithObject:#"" forKey:#"parameters"];
[[RKClient sharedClient] put:yourURL params:params delegate:self];
Of course you will need to have RestKit imported, and your .h file must accept < RKRequestDelegate >. I hope that gets you on the right track.
You may also want to look at this SO question, however I haven't implemented the suggested solution so I can't ascertain as to its efficacy.
I'm just doing my first steps in Core Data and would like to change a piece of my code which uses a simple plist to store some preference data of a program. I would like to use Core Data instead. Just for educational reason.
So basically I created an entity named "setup" with a bunch of attributes like "language", "debug info", "server ip" and so on. There should be only one data record in the entity as there is no sense to keep multiple setups.
I then created the GUI in the interface builder by alt-dragging the entity over the GUI window. I choosed "single item view". I can run the code but there is a problem:
I'm not able to change the content of the contols. I assume it's simply because there is no record in the entity. So my question is, how to create a "default" entry? I don't want to use the "Master/Detail view" as I simply need just one data record which should be generated automatically in case the storedata-file isn't created yet.
I solved the problem so far. I added the following lines to the method "- (NSManagedObjectContext *) managedObjectContext". It creates a default record in the entity on the first call of the method. The values of the attributes are set up in the data model.
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:#"Setup" inManagedObjectContext:managedObjectContext]];
[request setIncludesSubentities:NO]; //Omit subentities. Default is YES (i.e. include subentities)
NSError *err;
NSUInteger count = [managedObjectContext countForFetchRequest:request error:&err];
if(count==NSNotFound || count==0)
{
[NSEntityDescription insertNewObjectForEntityForName:#"Setup" inManagedObjectContext: managedObjectContext];
}
[request release];
I'm not sure if it's the correct way to handle it, especially I doubt that's the right place. I'm missing the method which is called after the Core Data has been set up. Any suggestions are highly welcome!
I've read through the KVC docs on Apple and it talks in depth about making your indexed collections accessible through key value coding, but I can't find any examples of a key path being used to access an arbitrary element within the array.
If my Blob class has an NSArray *widgets, I'd like to be able to get the widget at index 4 by doing something like:
[myBlob valueForKeyPath:#"widgets[4]"]
Is there anything like this?
myBlob answers to 'valueForKey:' and widgets being an NSArray answers to 'objectAtIndex:'.
So '[[myBlob valueForKey:#"widgets"] objectAtIndex:4]' should do the trick.
hello i have this array that compares two arrays to see if it contains an object however after testing it with nslog
NSLog (#"1");
favoritesArray = [[NSMutableArray alloc] init];
NSLog (#"2");
//Add items
// favoritesArray = [[NSMutableArray alloc]init];
didContain = [[NSMutableArray alloc]init];
NSLog (#"3");
if ([favoritesArray containsObject:#"one"])
{
[didContain addObject:#"one"];
NSLog (#"4");
}
the 4th log doesn appear.
does this mean my implementation is wrong of the if it contains an object or it means there is no file present like that?
EDIT
here is where i add an array
-(IBAction) addToFavorites {
favoritesArray = [[NSMutableArray alloc] init];
[favoritesArray addObject:#"one"];
NSLog (#"hello");
how come this doesnt show up as the object #"one"
The fourth log doesn't appear because the array doesn't contain the object #"one" -- the NSLog is inside the if statement testing for that case.
With the code as shown, that's correct -- the array will be empty and so it's not surprising there's no #"one".
Presumably the real code does something to put stuff in the array, but since we can't see that it's hard to say anything about it.
EDIT: Although the code you've added to the question for addToFavorites seems straightforward enough, it's still not clear how it relates to the original code. Where is it called from? What is the sequence of events?
One issue in particular is that you seem to be allocating a new array in both locations. If this is the case, then the reason you don't see #"one" appearing is that you are adding it in one array and looking for it in a different one. The fact that they both happen to be called favoritesArray is incidental and just muddies the water.
In fact, it is extremely likely that you shouldn't be allocating the array in either location, but should just be referencing an array that gets allocated only once somewhere else (eg, in your class's designated init method) and then kept as an ivar for the lifetime of your application.
I have a Core Data based mac application that is working perfectly well until I save a file. When I save a file it seems like something changes in core data because my original fetch request no longer fetches anything. This is the fetch request that works before saving but returns an empty array after saving.
NSEntityDescription *outputCellEntityDescription = [NSEntityDescription entityForName:#"OutputCell"
inManagedObjectContext:[[self document] managedObjectContext]];
NSFetchRequest *outputCellRequest = [[[NSFetchRequest alloc] init] autorelease];
[outputCellRequest setEntity:outputCellEntityDescription];
NSPredicate *outputCellPredicate = [NSPredicate predicateWithFormat:#"(cellTitle = %#)", outputCellTitle];
[outputCellRequest setPredicate:outputCellPredicate];
NSError *outputCellError = nil;
NSArray *outputCellArray = [[[self document] managedObjectContext] executeFetchRequest:outputCellRequest
error:&outputCellError];
I have checked with [[[self document] managedObjectContext] registeredObjects] to see that the object still exists after the save and nothing seems to have changed and the object still exists. It is probably something fairly basic but does anyone know what I might be doing wrong? If not can anyone give me any pointers to what might be different in the Core Data model after a save so I might have some clues why the fetch request stops working after saving?
Edit
I have got as far as working out that it is the relationships that seem to be breaking after a save. If I omit the lines setting a predicate for the request, the request returns objects in the array. I have checked through the registeredObjects and it appears that the relationships are intact, but if I do something like save a file, re-open it and then check the registeredObjects the relationships are set to nil. I've opened a save file as an xml file and the relationships appear to be intact when the file is first saved.
I've added a screen shot of the part of the core data model were the relationships are broken. Does anyone have any idea why saving a file in core data might break the relationships? For reference I'm using the default implementation of save built into core data so there is no custom save code.
http://emberapp.com/splash6/images/littlesnapper/sizes/m.png
Edit
I have no -awakeFromFetch: methods that are triggering when this problem is caused.
I have sub-classed NSManagedObject for some of the problem objects, using the Core Recipes model for KVO:
+(void)initialize
{
if (self == [OutputCell class])
{
NSArray *nameKeys = [NSArray arrayWithObjects:#"cell", #"sheet", #"table", nil];
[self setKeys:nameKeys
triggerChangeNotificationsForDependentKey:#"cellTitle"];
NSArray *measuresKeys = [NSArray arrayWithObjects:#"fivePercentile", #"maximum", #"mean", #"median",#"minimum",#"ninetyFivePercentile",#"standardDeviation",nil];
[self setKeys:measuresKeys
triggerChangeNotificationsForDependentKey:#"analysisResults"];
}
}
This method doesn't seem to be firing during or after a save so it doesn't seem to be this that is causing the problem. I'm currently going through all the other methods in the code to find if any of them happen to get called during or after a save.
Edit
Following on from Marcus' suggestion below I've managed to make a fetch request fail before the model is saved. My problem now is the message this getting returned in the console when it fails:
HIToolbox: ignoring exception '+entityForName: could not locate an NSManagedObjectModel for entity name 'OutputCell'' that raised inside Carbon event dispatch
The console message is logged following this call:
NSEntityDescription *outputCellEntityDescription = [NSEntityDescription entityForName:#"OutputCell"
inManagedObjectContext:[[self document] managedObjectContext]];
Should the extra ' following the OutputCell in the console message be there? Do objects normally have this extra ', or has it come from somewhere? If it has come from somewhere and is causing the fetch request to fail, does anyone have any bright ideas where this might have come from or how I might track it's source down?
Sounds like you are setting something to nil somewhere and causing you to get nil back. I would walk through your save and fetch code in the debugger and look for objects being set to nil when you do not expect it.
update
Do you have any code anywhere that can be manipulating the relationships? Perhaps something in the -awakeFromFetch: that is causing the relationships to get corrupted?
If they are saving correctly the first time and then failing then that truly points at something in your code corrupting those relationships. Are you subclassing NSManagedObject for these objects? If so are you by chancing overriding the -init... method?
update
That last tick should definitely not be there. Check your fetch request, this might all boil down to a simple typo in a string somewhere...