Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Problem with cocoa using system() and NSTextView

Problem with cocoa using system() and NSTextView
Thread Tools
Fresh-Faced Recruit
Join Date: May 2005
Status: Offline
Reply With Quote
May 28, 2005, 08:03 PM
 
Hello,
Part of the app I'm writing is basically the commandline with an aqua makeover. However, system() function isn't behaving like I would expect. The code given at the bottom is the for the command action, which is whenever someone presses enter from the command field or presses the Send button in the gui. However, when I type "w" (then Send) and then "vim" (then Send) the following errors are reported. I get these same errors no matter

[Session started at 2005-05-28 19:24:39 -0500.]
sh: line 1: \240\364: command not found
sh: line 1: \240\364: command not found

I know all my outlets, buttons and actions are all properly connected. I am also having a problem with outputting to the NSTextView object called resultTextView. For instance, the code seems like it should output "TEST" every time a command is given, but instead it does nothing.

- (IBAction)command: (id)sender
{
NSString *cmd = [[NSString alloc] init];
NSMutableString *result = [[NSMutableString alloc] init];
cmd = [commandField stringValue];
system((char *)cmd);
[result setString:@"TEST"];
[resultTextView insertText:result];
}

Sorry if this is really amateur...I tried looking stuff up on the apple website and other posts, but it didn't help all that much. I figured I'd get some people's input. Thanks!
     
Senior User
Join Date: Nov 2001
Location: State of Denial
Status: Offline
Reply With Quote
May 28, 2005, 10:50 PM
 
cmd is a pointer to an NSString, not a buffer. You need to be using something like system([cmd UTF8String]); .

You should also be releasing cmd and result before the end of the method, since they're not autoreleased and you're not storing their pointers anywhere visible outside of the method's scope.
[Wevah setPostCount:[Wevah postCount] + 1];
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
May 29, 2005, 12:44 AM
 
The inital assignment of cmd to [[NSString alloc] init] is also a memory leak, since you immediately turn around and assign it to [commandField stringValue] without ever releasing that initial blank string. Just make it NSString *cmd = [commandField stringValue].

Also, just to offer more detail on why it isn't working: You can't cast objects to different types in Objective-C. The variables you use to refer to objects are all pointers (e.g. NSString*), so casting it to another type (e.g. char*) just fools the compiler without actually performing any sort of conversion. In order to convert an object, you should call the appropriate method (e.g. [myString UTF8String]).
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
May 29, 2005, 09:33 AM
 
And you should probably be using NSTask, anyway. system() is horrible.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 07:34 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2