- (IBAction)sendMessage:(id)sender
{
NSString* conversationFile = [#"~/" stringByAppendingPathComponent:#"conversation.txt"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:conversationFile];
if (fileExists == FALSE)
{
[self doShellScript:#"do shell script \"cd ~/; touch conversation.txt\""];
}
NSString *conversationContent = [[NSString alloc] stringWithContentsOfFile:#"~/conversation.txt" encoding:NSUTF8StringEncoding error:NULL];
NSString *myMessage = [[messageBox stringValue]copy];
NSString *combinedContent = [NSString stringWithFormat:#"%# \r\n %#", conversationContent, myMessage];
[[[myConversationBox textStorage] mutableString] setString: combinedContent];
[combinedContent writeToFile:#"~/conversation.txt" atomically:YES encoding:NSUTF8StringEncoding error:NULL];
}
The above code presents the following error
2011-07-07 21:38:08.703
iMessages[86493:903]
-[NSPlaceholderString stringWithContentsOfFile:encoding:error:]:
unrecognized selector sent to instance
0x100111690
2011-07-07 21:38:08.704
iMessages[86493:903]
-[NSPlaceholderString stringWithContentsOfFile:encoding:error:]:
unrecognized selector sent to instance
0x100111690
stringWithContentsOfFile:encoding:error: is a class method of NSString, not an instance method, so you don't need to (shouldn't) alloc it first.
NSString *conversationContent = [NSString stringWithContentsOfFile:#"~/conversation.txt" encoding:NSUTF8StringEncoding error:NULL];
Use initWithContentsOfFile in place of stringWithContentsOfFile or remove the alloc call. So have:
NSString *conversationContent = [[NSString alloc] initWithContentsOfFile:#"~/conversation.txt" encoding:NSUTF8StringEncoding error:NULL];
or
NSString *conversationContent = [NSString stringWithContentsOfFile:#"~/conversation.txt" encoding:NSUTF8StringEncoding error:NULL];
Related
I want to be able to say "when nextButton is pressed, output". I have an IBAction for the button too, I don't know what to do with it.
NSURL *myURL = [[NSBundle mainBundle]
URLForResource: #"artList" withExtension:#"txt"];
NSError *error = nil;
NSString *string = [NSString stringWithContentsOfURL:myURL encoding:NSUTF8StringEncoding error:&error];
if (string) {
NSArray *lines = [string componentsSeparatedByString:#"\n"];
//output first definition to screen
_wordDefBox.text = [NSString stringWithFormat:#"%#%#",_wordDefBox.text, lines[0]];
_wordDefBox.text = [NSString stringWithFormat:#"%#%s",_wordDefBox.text, "\n"];
_wordDefBox.text = [NSString stringWithFormat:#"%#%#",_wordDefBox.text, lines[1]];
}
//error checking
else {
NSLog(#"Unable to load string from %#: %#", myURL, error);
}
I need help identifying my error. Does anyone know why it errors?
if (result == NSFileHandlingPanelOKButton) {
NSString *filePath = [[openPanel URLs] objectAtIndex:0];
NSLog(#"%#", filePath);
*CODE WORKS UP TO HERE*
NSString *strTemp = [self extractString:filePath toLookFor:#"//" skipForwardX:2 toStopBefore:#".png"];
NSLog(#"%#",strTemp);
NSString *copyScript = [NSString stringWithFormat:#"%# /tmp/%#.png", strTemp, myString];
strTemp = [[NSString alloc] init];
NSString *path = #"/Applications/APP.app/Contents/Resources/copy.sh";
NSArray *args = [NSArray arrayWithObjects:copyScript, nil];
[[NSTask launchedTaskWithLaunchPath:path arguments:args] waitUntilExit];
NSString* linkName = #"/tmp/";
NSString* extension = #".png";
NSString* fullPath = [NSString stringWithFormat:#"%#%#%#", linkName, myString, extension];
urlPathOfFile = [NSString stringWithFormat:#"URL/%#%#", myString, extension];
fileURL = [NSURL URLWithString:fullPath];
action = upload;
[self runAction];
Is it a memory error because I have too many strings?
There's a high probability that filePath points to a NSURL object, not NSString.
read in did load:
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path]) //4
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:#"data" ofType:#"plist"]; //5
[fileManager copyItemAtPath:bundle toPath: path error:&error]; //6
}
//load in text fields.
NSMutableDictionary *savedData = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
nameField.text = [[savedData objectForKey:#"name"] stringValue];
locationTextField.text = [[savedData objectForKey:#"location"] stringValue];
sectorTextField.text = [[savedData objectForKey:#"sector"] stringValue];
Write on button click:
- (IBAction)writingButton:(id)sender
{
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
//[data setObject:[NSNumber numberWithInt:value] forKey:#"value"];
[data setObject:[NSString stringWithString:nameField.text] forKey:#"name"];
[data setObject:[NSString stringWithString:locationTextField.text] forKey:#"location"];
[data setObject:[NSString stringWithString:sectorTextField.text] forKey:#"sector"];
}
the plist file:
The error:
2012-09-04 17:03:40.360 app[4849:c07] -[__NSCFString stringValue]:
unrecognized selector sent to instance 0x6aa38f0 2012-09-04
17:03:40.362 app[4849:c07] *** Terminating app due to uncaught
exception 'NSInvalidArgumentException', reason: '-[__NSCFString
stringValue]: unrecognized selector sent to instance 0x6aa38f0'
Any ideas? cheers.
The dictionary stores NSString objects directly - there's no need to call a (nonexistent) method called - stringValue. Simply write
nameField.text = [savedData objectForKey:#"name"];
and so on.
(Why do you think this method call would have been necessary?)
i'm pulling a 2 dimensional array from a remote server:
- (NSMutableArray*)qBlock{
NSURL *url = [NSURL URLWithString:#"http://wsome.php"];
NSError *error;
NSStringEncoding encoding;
NSString *response = [[NSString alloc] initWithContentsOfURL:url
usedEncoding:&encoding
error:&error];
const char *convert = [response UTF8String];
NSString *responseString = [NSString stringWithUTF8String:convert];
NSMutableArray *sample = [responseString JSONValue];
return sample;
}
and putting them into:
NSMutableArray *qnBlock1 = [self qBlock];
NSString *answer1 = [NSString stringWithFormat:[[qnBlock1 objectAtIndex:0]objectAtIndex:1]];
answer = [[NSMutableDictionary alloc]init];
[answer setObject:answer1 forKey:#"1"];
question1.text = [[qnBlock1 objectAtIndex:0] objectAtIndex:0];
label1a.text = [[qnBlock1 objectAtIndex:0]objectAtIndex:2];
label1b.text = [[qnBlock1 objectAtIndex:0]objectAtIndex:3];
label1c.text = [[qnBlock1 objectAtIndex:0]objectAtIndex:4];
label1d.text = [[qnBlock1 objectAtIndex:0]objectAtIndex:5];
I received this error during runtime
-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x6c179502012-04-30 09:43:50.794 AppName[371:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x6c17950'
Is this due to syntax issue for 2 dimensional array?
You're not getting back a multi-dimensional array. You're getting back an array of NSDictionary objects. The error you got indicates that you're trying to send the message objectAtIndex: to an NSDictionary object, which fails since it has no such selector.
Update:
After chatting on the side, it became apparent that the following was true:
The user was using the SBJson library to parse the return value from his php web service.
The return value was either:
An NSDictionary with each key being the text representation of its (non-index-based) location in the list (#"1", #"2", etc...) and each value being an NSArray of NSString objects, or
An NSArray of NSString objects (seems to be the way a single "answer" returns)
Here's the code I supplied to let him iterate through his return value:
NSURL *url = [NSURL URLWithString:#"{his url}"];
NSError *error;
NSStringEncoding encoding;
NSString *response = [[NSString alloc] initWithContentsOfURL:url usedEncoding:&encoding error:&error];
const char *convert = [response UTF8String];
NSString *responseString = [NSString stringWithUTF8String:convert];
NSLog(#"%#", responseString);
SBJsonParser *parser = [[SBJsonParser alloc] init];
id sample = [parser objectWithString:responseString];
if ([sample isKindOfClass:[NSDictionary class]]) {
for (id item in sample) {
NSLog(#"%#", [item description]);
NSArray *subArray = [sample objectForKey:item]; // b/c I know it's an array
for (NSString *str in subArray) {
NSLog(#"item: %#", str);
}
}
} else if ([sample isKindOfClass:[NSArray class]]) {
for (NSString *str in sample) {
NSLog(#"item: %#", str);
}
}
Hope it helps, J!
I think from your error message, you're sending objectAtIndex to a dictionary. NSDictionary doesn't have such a method. You should use objectForKey instead.
I've an NSOpenPanel called oPanel. From it, I get the path of a folder. As I use URLs (instead of the deprecated filenames) I want to get rid of the beginning (file://localhost).
But I have the following error that I can't understand:
2011-07-29 18:01:45.587 RedimV3[12857:407] -[NSURL length]: unrecognized selector sent to instance 0x1023543d0
2011-07-29 18:01:45.588 RedimV3[12857:407] -[NSURL length]: unrecognized selector sent to instance 0x1023543d0
Here is the code:
NSArray *files = [oPanel URLs];
NSLog(#"before: %#", [files objectAtIndex:0]);
NSMutableString *temp = [[NSMutableString alloc] initWithString:[files objectAtIndex:0]];
[temp deleteCharactersInRange:NSMakeRange(0,15)];
NSLog(#"after: %#",temp);
The first NSLog works, the second doesn't.
I will be glad if you can help me, thanks.
[files objectAtIndex:0] is probably an NSURL, not an NSString. Try using [[files objectAtIndex:0] path] instead. In fact, if you use -path you won't even have to worry about the file:// part.
If you just want the file name then you would do as #jtbandes said with a minor correction to use the lastPathComponent instance method of NSString
Quick and dirty version
NSArray *files = [oPanel URLs];
NSLog(#"before %#",[files objectAtIndex:0]);
NSString *fileName = [[[files objectAtIndex:0] path] lastPathComponent];
NSLog(#"after :%#",fileName);
More readable code version
NSArray *files = [oPanel URLs];
NSURL *file = [files objectAtIndex:0];
NSLog(#"before %#",file);
NSString *localPath = [file path];
NSString *fileName = [localPath lastPathComponent];
NSLog(#"after :%#",fileName);
You need to turn the NSURL into a string...
NSURL *myURL = [files objectAtIndex:0];
NSMutableString *string = [NSMutableString stringWithString:[myURL absoluteString]];
[string deleteCharactersInRange:NSMakeRange(0,15)];
NSLog(#"after: %#", string);