Adding CoreData to existing Project - objective-c

I am trying to add CoreData to an existing project and
there is strange error...
I get the error:
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason:
'Cannot create an
NSPersistentStoreCoordinator with a
nil model'
in RootView there is a warning in:
- (NSFetchedResultsController *)fetchedResultsController;
in line:
_fetchedResultsController.delegate = self;
and it says:
warning: class 'RootViewController'
does not implement the
'NSFetchedResultsControllerDelegate'
protocol
any ideas?

argh!
sorry for even asking, I just missed that I changed the name of .xcdatamodel
I needed just to change the name in managedObjectModel and persistentStoreCoordinator
hope it'll help someone anyway.

I was getting the same exception.
My issue was that I had added the .xcdatamodeld file in the root directory of the project (with siblings Frameworks and Products) instead of in the folder named after the project (with siblings Supporting Files, AppDelegate.h, etc.)
I tracked it down to this line: NSURL *modelURL = [[NSBundle mainBundle] URLForResource:#"Foo" withExtension:#"momd"];
In this method: - (NSManagedObjectModel *)managedObjectModel

Related

Objective-C errors after getting the IAP product response

This code is from the Phonegap Code: IAP Plugin. The error happens on the line of the code right after the "sent js". All the elements sent to the function are non-nil except for the last one 'nil'. I even logged them out to make sure they were sent. This code is right out of the plugin (https://github.com/usmart/InAppPurchaseManager-EXAMPLE) and has not been modified except for the logging. In the debugger i saw that none of the objects were nil, so i don't understand why the error is happening.
Here is the error:
[__NSArrayI JSONRepresentation]: unrecognized selector sent to
instance 0xdc542d0
2013-02-13 23:26:17.209 GoblinSlots[4519:707] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
'-[__NSArrayI JSONRepresentation]: unrecognized selector sent to
instance 0xdc542d0'
here is the code:
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse: (SKProductsResponse *)response
{
NSLog(#"got iap product response");
for (SKProduct *product in response.products) {
NSLog(#"sending js for %#", product.productIdentifier);
NSLog(#" title %#", product.localizedTitle );
NSLog(#" desc%# - %#", product.localizedDescription, product.localizedPrice );
NSArray *callbackArgs = [NSArray arrayWithObjects:
NILABLE(product.productIdentifier),
NILABLE(product.localizedTitle),
NILABLE(product.localizedDescription),
NILABLE(product.localizedPrice),
nil ];
NSLog(#"sent js");
NSString *js = [NSString stringWithFormat:#"%#.apply(plugins.inAppPurchaseManager, %#)", successCallback, [callbackArgs JSONSerialize]];
NSLog(#"js: %#", js);
[command writeJavascript: js];
}
All the stuff to do JSON serialization seems to be already included with the Cordova plugins.
There's no need to download and install yet another JSON library.(a)
It appears that PhoneGap is in the process of switching from SBJson to JSONKit.(b)
PhoneGap is also in the process of changing all the JSON methods to use a "cdvjk_" prefix. (c)
As far as I can tell, something didn't quite go right during those changes.
What I did was edit the file Plugins/InAppPurchaseManager.m , where I made these changes:
Add the line
#import <Cordova/CDVJSON.h>
Replace the line
return [self respondsToSelector:#selector(cdvjk_JSONString)] ? [self cdvjk_JSONString] : [self cdvjk_JSONRepresentation];
with
return [self JSONString];
. (What's the right way to push this or a better bugfix back to the nice PhoneGap people?)
JSONRepresentation is a category that SBJson adds so you have to include SBJson.h in the class that uses it.

trouble debugging during runtime

I have written a some code for a drill down table app I have been making, but the app crashes only during runtime. Xcode doesn't give me any errors while building the app. The debugger outputs:
2012-10-18 10:58:26.513 second[474:c07] -[NavController setItems:]: unrecognized selector >sent to instance 0xc217a00
2012-10-18 10:58:26.515 second[474:c07] * Terminating app due to uncaught exception >'NSInvalidArgumentException', reason: '-[NavController setItems:]: unrecognized selector sent >to instance 0xc217a00'
* First throw call stack:
(0x14b8022 0xeb8cd6 0x14b9cbd 0x141eed0 0x141ecb2 0x3fbe 0xe2a1e 0x41401 0x41670 0x41836 >0xbfc9dd8 0x4872a 0x19596 0x1a274 0x29183 0x29c38 0x1d634 0x13a2ef5 0x148c195 0x13f0ff2 >0x13ef8da 0x13eed84 0x13eec9b 0x19c65 0x1b626 0x1d40 0x1cd9)
terminate called throwing an exception
I think I understand that the error lies in NavController.m where:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString* path = [[NSBundle mainBundle] pathForResource:#"Data" ofType:#"plist"];
MasterViewController* root = (MasterViewController*)self.topViewController;
NSDictionary* thelist = [NSDictionary dictionaryWithContentsOfFile:path];
root.items = [thelist objectForKey:#"Items"];
root.navigationItem.title = [thelist objectForKey:#"name"];
}
btw, I made the array items like this: (nonatomic, retain) NSArray* items;
The app crashes, because of the following line:
root.items = [thelist objectForKey:#"Items"];
This line is just a shortcut for writing:
[root setItems:[thelist objectForKey:#"Items"]];
And the runtime complains that there is no method setItems: found. The reason why there is compile error is because you tell the compiler that root is of class MasterViewController, however, that is not true. At runtime the obj-c runtime finds out that root is in fact of class NavController and it seems like NavController has no setItems: method.
In other words, this line is wrong:
MasterViewController* root = (MasterViewController*)self.topViewController;
You lie about the class; self.topViewController returns an object that is of class NavController, yet you force the compiler to treat it as MasterViewController which will fail as soon as you call a method that is not found for NavController.
The problem is probably here:
MasterViewController* root = (MasterViewController*)self.topViewController;
self.topViewController is proably not an instance of MasterViewController, but a NavController instance and there you go.

Could not load NIB in bundle ImageViewCell

I'm getting this error while running simulator. The project was not created on this environment, not sure I'm missing a configuration setting.
Error
2012-08-08 19:30:56.411 ACME[4068:f803] mItemArray.count: 2 2012-08-08 19:30:56.413 ACME[4068:f803] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle (loaded)' with name 'ImageViewCell''
if(cell == nil)
{
NSArray *array = [[NSBundle mainBundle] loadNibNamed:#"ImageViewCell" owner:self options:nil];
cell = [array objectAtIndex:0];
}
You have not set the "files owner" in the NIB file to the owner you are setting in code. The NIB loader will fail like this when it cant link the objects in the NIB to the right root owner.
The name after "loadNibNamed:" must be the name of the nib file (in project navigator on the left side of the screen), sometimes people write there 'cell identifier' that writes from the start in CellForRowAtIndexPath (static NSString ...).
May be your problem in this little fault?

Error implementing UIWebView to load local html

am trying to add a UIWebView to my current UIScrollView. The UIWebView is supposed to access a local html file 'index.html'. I got an error at runtime:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'
This is my code:
UIWebView *webView = [[UIWebView alloc] initWithFrame:scrollViewFrame];
NSString *indexPath = [NSBundle pathForResource:#"index" ofType:#"html" inDirectory:#"Addition"];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:indexPath]]];
I do have 'Addition' folder (not group; imported from 'Add' in my project).
Thanx in advance...
Did you check indexPath with a breakpoint or NSLog? It looks like your file is not there (perhaps a typo?)-
EDIT
OK, I just tested this successfully. Try this:
Delete all the web files from your project.
Copy the web directory into the right place in your project folder.
Drag from Finder to Xcode, do not copy, Create folder references
One more caveat: Maybe there is a difference in capitalized resource names between the simulator and the device. Try using lowercase throughout.
Also, you should perhaps correct your command pulling the resource from the bundle. Instead of
[NSBundle pathForResource...
it should be
[[NSBundle mainBundle] pathForResource...

ObjC / iOS: NSCFString not NSString?

I'm making a small iOS application which requests a JSON file with ASIHTTPRequest and I want it to be parsed by JSONKit. I'm using the following code:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
// yada yada
NSURL *url = [NSURL URLWithString:#"http://localhost/file.json"]; // the file is a valid Json
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request setTimeOutSeconds:20]; // Response takes too long on local machine
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request {
responseString = [request responseString]; // request type: ASIREQUEST, responseString is declared as NSString
NSLog(#"%# \n", [responseString class]); // NSCFString
NSDictionary *deserializedData = [responseString objectFromJSONString];
}
However, I'm seeing the following exception when the application runs:
[7646:207] -[NSCFString objectFromJSONString]: unrecognized selector sent to instance 0xdba0000
[7646:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-`[NSCFString objectFromJSONString]: unrecognized selector sent to instance 0xdba0000'`
What could be causing this exception? Why is it showing that I'm using an NSCFString here, even though I'm specifying an NSString?
NSString is something known as a class cluster. What this basically means is that when you construct an NSString you will get back something that is guaranteed to behave like an NSString as defined in its documentation, but could be implemented by some other class. In this case NSCFString is the type the system decided to return.
Anything that takes an NSString will take anything that is part of that cluster.
From looking at your code, ASI must define objectFromJSONString as part of a category on NSString. This will work perfectly fine with the clustering system, are you loading a static library that includes ASI? If so you need to add -all_load and -ObjC to your linker flags in order to get categories working from external libraries.
Edit:
Based on your comments then your solution is making sure the JSON library is included in your project. I'm assuming the objectFromJSONString was copy/pasted from some sample somewhere. It is part of the JSONKit library. Make sure both JSONKit.m and JSONKit.h are present in your project.
Update:
JSONKit is included (both .h and .m) but still the same error...
you are right, i just copied it because it seemed to do its job :)
You've included JSONKit.h, but you forgot to include JSONKit.m in the project. It compiles fine, but the implementation isn't there at runtime.
I had same problem because I enabled ARC and JSonKit doesn't support it after rectifying it worked.
Also when you include JSonKit.h and .m files please make sure you check target box too.
JSONKit declares a category called JSONKitDeserializing on NSString in order to allow you to call objectFromJSONString. Make sure you've included JSONKit.h in your source file where you are trying to use objectFromJSONString.