Error about FMDB(Objective-c Database Library) - objective-c

I'm developing iOS App with FMDB(Database Library of using SQLite easily).
For creating a database named "test.db", I'm writing down the following code for making the path of the database.
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *paths = [manager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
NSString *documentDirs = [paths objectAtIndex:0];
NSString *writableDBPath = [documentDirs stringByAppendingPathComponent:#"test.db"];
When I tested with Xcode Simulator, I got the following error.
2014-07-13 21:27:44.084 ninethtest[1481:a0b] -[NSURL stringByAppendingPathComponent:]: unrecognized selector sent to instance 0x8a5fdd0
2014-07-13 21:27:44.095 ninethtest[1481:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL stringByAppendingPathComponent:]: unrecognized selector sent to instance 0x8a5fdd0'
Could you tell me how to solve this error.

You are mixing-up NSString and NSURL objects. You want:
NSURL *documentDirs = [paths objectAtIndex:0];
NSURL *writableDBPath = [documentDirs URLByAppendingPathComponent:#"test.db"];
and if the FMDB API requires an NSString object for the database path, you can extract an NSString object from an NSURL object using:
NSString *writableDBPathString = [writableDBPath path];

Related

how to edit a plist programmatically in xcode 5 iOS 7?

how can I edit or change a value string from:
in this plist I need change or Set "NO" in Enabled Value from Item 2, I see a lot examples, but not working, or are deprecated, or don't use ARC, any idea or example code? please help guys
EDIT#1: I try this (not work)
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
path = [path stringByAppendingPathComponent:#"List.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:path]) {
NSString *sourcePath = [[NSBundle mainBundle] pathForResource:CONTENT_DEFAULT ofType:PLIST];
[fileManager copyItemAtPath:sourcePath toPath:path error:nil];
}
NSArray* newContent = [[NSArray alloc]initWithContentsOfFile:path];
NSDictionary *Dict = [[NSDictionary alloc]initWithDictionary:[newContent objectAtIndex:2]];
[Dict setValue:#"NO" forKey:#"Enabled"];
[Dict writeToFile:path atomically:YES];
NOTE: not work, it crashes send me:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSDictionaryI 0x15de7ff0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Enabled.', or Im wrong in something?
EDIT #2 new Try
NSString *path = [[NSBundle mainBundle] pathForResource:#"List" ofType:#"plist"];
NSString *savingPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSArray* newContent = [[NSArray alloc]initWithContentsOfFile:path];
NSMutableDictionary *Dict = [[NSMutableDictionary alloc]initWithDictionary:[newContent objectAtIndex:2]];
[Dict setValue:#"NO" forKey:#"Enabled"];
savingPath = [savingPath stringByAppendingPathComponent:#"Modified.plist"];
[Dict writeToFile:savingPath atomically:YES];
NSLog(#"newContent:%#",newContent);
NSArray *newnew = [[NSArray alloc]initWithContentsOfFile:savingPath];
NSLog(#"newnew:%#",newnew);
NOTE: now print me a crash:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (3) beyond bounds (2)'
All you have to do is to use NSMutableDictionary instead of a regular dictionary to modify stuff. You have to always check that the object you are editing is there.
To get the right path for the plist you created in Xcode you need to use:
NSString *path = [[NSBundle mainBundle] pathForResource:#"List" ofType:#"plist"];
You might actually want to save that file into the app's document directory. So you would use the following path for saving the content:
NSString *savingPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
savingPath = [savingPath stringByAppendingPathComponent:#"Modified.plist"];
[Dict writeToFile:savingPath atomically:YES];
Well, in your case the solution would be the following (when you have Dictionary as a member of Array):
NSString *path = [[NSBundle mainBundle] pathForResource:#"List" ofType:#"plist"];
NSArray *newContent = [[NSArray alloc]initWithContentsOfFile:path];
//iterating through all members since all of them has the string "YES" which should be replaced, otherwise, you will need to create a separate dictionary for each member
for ((i = 0; i < [newContent count]; i++)){
//here you take the member (dictionary) of the array
NSDictionary *dictOfArray = [newContent objectAtIndex:i];
[dictOfArray objectForKey:#"Enabled"]=#"NO";
}
//if you update the same pList you can use the same path (otherwise use another path)
[newContent writeToFile:path atomically:YES];
So, it works now.

Encode array of images exception

i try to encode an array with UIImages.
Then i call "save" function
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:#"SavedUserData"];
[NSKeyedArchiver archiveRootObject: self toFile:filePath];
i get exception for line
[encoder encodeObject:recipePhotoList forKey:#"recipePhotoList"]; //array with images
-[UIImage encodeWithCoder:]: unrecognized selector sent to instance 0x1b1380
2012-03-23 11:59:43.694 fishmarket[261:1a0f] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImage encodeWithCoder:]: unrecognized selector sent to instance 0x1b1380'
On 5+ iOS its working perfectly, on 4.3 i have this problem.
That's because UIImage didn't support NSCoding until iOS 5. Check the iOS 5.0 API diff.
some alternative to encode/decode UIIMage for iOS < 5.0
+ (void)saveImage:(UIImage*)image withName:(NSString*)imageName
{
if (image != nil)
{
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithString:imageName]];
NSData* data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
}
}
+ (UIImage*)loadImageNamed:(NSString*)imageName
{
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithString:imageName]];
UIImage* image = [UIImage imageWithContentsOfFile:path];
return image;
}
hope its help somebody.

SBJSON parsing NSString to NSDictionary

I'm trying to parse an NSString that contains JSON data into an NSDictionary using SBJson 3.0.4, but when I do it, I get this error:
"WebKit discarded an uncaught exception in the webView:shouldInsertText:replacingDOMRange:givenAction: delegate: -[__NSCFString JSONValue]: unrecognized selector sent to instance 0x6ab7a40"
As far as I know (which isn't very far), the JSON I'm getting is valid, so I don't know why this is happening. My code compiles fine too…
Here it is:
NSString *tempURL = [NSString stringWithFormat:#"http://maps.googleapis.com/maps/api/geocode/json?address=%#&sensor=true",userInput.text];
NSURL *url = [NSURL URLWithString:tempURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:30];
// fetch the JSON response
NSData *urlData;
NSURLResponse *response;
NSError *error;
// make the synchronous request
urlData = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&response
error:&error];
// construct a String around the Data from the response
NSString *data = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
NSDictionary *feed = [data JSONValue];
The important part of the error message is this:
-[__NSCFString JSONValue]: unrecognized selector sent to instance 0x6ab7a40
The __NSCFString class is the private implementation class for the NSString interface, so you can just pretend it says NSString.
So we see that you are sending the JSONValue message to an NSString, and the NSString says it doesn't recognize that selector. The SBJson library adds a JSONValue method to the NSString class using a category.
So I deduce that you haven't linked NSObject+SBJson.o into your app. If you copied the SBJson source files into your app, make sure you copied in NSObject+SBJson.m, and make sure it is included in the “Compile Sources” build phase of your target.
If you built an SBJson library and are linked your app to that, you may need to add the -ObjC flag to your linker options, or even the -all_load flag.

copy all files with extension to documents directory

I am having a little bit trouble with copying files from an online place to the Documents Directory of the iPad in xcode.
The files that i want to download had to be the files with the extension 'xml'.
now i am doing this:
NSData *onlineLink = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://localhost:8888/"]];
NSString *extension = #"xml";
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *contents = [fileManager contentsOfDirectoryAtPath:onlineLink error:NULL];
NSEnumerator *e = [contents objectEnumerator];
NSString *filename;
while ((filename = [e nextObject])) {
if ([[filename pathExtension] isEqualToString:extension]) {
[fileManager copyItemAtPath:[documentsDirectory stringByAppendingPathComponent:filename toPath:documentsDirectory error:NULL]];
}
}
However this does not work, i get this error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteData fileSystemRepresentation]: unrecognized selector sent to instance 0xb000800'
Can anybody give me a hint why i am getting the error?
in the line:
NSArray *contents = [fileManager contentsOfDirectoryAtPath:onlineLink error:NULL];
contentsOfDirectoryAtPath should be an NSString, you are passing an NSData object

unable to play audio file from document?

when ever i try to play audio this error appears
2010-08-27 09:13:40.466 VoiceRecorder[3127:207] Failed with reason: The operation couldn’t be completed. (OSStatus error -43.)
what does it mean
I am physically storing file in Iphone Document folder and copying its name in my database
so when ever I play it from database its correctly get file path
and file name from document and database concat perfectly but unable to play
2010-08-30 12:22:00.592 VoiceRecorder[8140:207] /Users/Username/Library/Application Support/iPhone Simulator/4.0.1/Applications/4BF0B5C7-59C9-462F-94EC-
662EBCE8505E/Documents/30Aug10_12_21_58.aif
2010-08-30 12:22:00.593 VoiceRecorder[8140:207] 30Aug10_12_21_58.aif
2010-08-30 12:22:00.618 VoiceRecorder[8140:207] Failed with reason: The operation couldn’t be completed. (OSStatus error -43.)
need an urgent reply
here is my code
-(IBAction)playevent{
VoiceRecorderAppDelegate *appDelegate=(VoiceRecorderAppDelegate*)[[UIApplication sharedApplication]delegate];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *fullpath = [documentsDir stringByAppendingPathComponent:appDelegate.filenamefrompath];
NSFileManager *fileManager = [NSFileManager defaultManager];
// Check if the database has already been created in the users filesystem
BOOL success = [fileManager fileExistsAtPath:fullpath];
// If the database already exists then return without doing anything
if(success) { NSLog(#"Yes exists"); }else{ NSLog(#"no"); }
//return [documentsDir stringByAppendingPathComponent:appDelegate.filenamefrompath];
NSError* err;
NSLog(#"%#",fullpath);
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:fullpath] error:&err];
//NSString *filePath = [[NSBundle mainBundle] pathForResource:appDelegate.filenamefrompath ofType:#"aif"];
// NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
// player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
NSLog(#"%#",appDelegate.filenamefrompath);
player.delegate = self;
if( err ){
//bail!
NSLog(#"Failed with reason: %#", [err localizedDescription]);
}
[player play];
}
Please check my code for error
The code looks OK on the surface
Strangely tracking down the errors definitions is not entirely easy
They are in MacErrors.h
fnfErr = -43, /*File not found*/
Can you verify that the URL you are feeding the player in [NSURL fileURLWithPath:fullpath] is valid.
And more to the point is the file you are trying to play there in the Sim folder?
and off topic if this is supposed to be an IBAction then the signature should be
-(IBAction)playevent:(id)sender