Contribute
Register

Objective-C Programmers - Question.

Status
Not open for further replies.
Joined
Mar 3, 2010
Messages
72
Motherboard
GA-P55M-UD2
CPU
Intl Core i5
Graphics
EVGA GTX 750TI 2GB FTW
Mac
  1. MacBook Air
Classic Mac
  1. 0
Mobile Phone
  1. iOS
Hey guys,

I'm having a little problem here, i'm new to objective-c and crap that language is overwhelming.

Anyways, heres what's going on, hopefully someone can light the tunnel for me here.

In termnial i execute this command:
Code:
makehybrid -o ImageTest.iso /Users/Intrus/Desktop/newiso/ -iso -hfs -joliet -eltorito-boot newiso/cdboot -no-emul-boot -hfs-volume-name "TestDescription" -joliet-volume-name "TestDescription"

It create's the image just fine. I'm working on a little terminal program, to learn basically and i never though string manipulation could be this damn complicated.

Here's what i found after doing a bit of research, in order to execute shell commands you have to invoke NSTask....

Code:
        NSTask *task;
	task = [[NSTask alloc] init];
	[task setLaunchPath: @"/usr/bin/hdiutil"];
	
	
	NSArray *arguments;
	arguments = [NSArray arrayWithObjects: @" makehybrid -o ",bootDiskName, holdFolderPath, @"/ -iso -hfs -joliet -eltorito-boot newiso/cdboot -no-emul-boot -hfs-volume-name ", @"\"",bootDescription,@"\"", @" -joliet-volume-name ", @"\"",bootDescription, @"\"", nil];
	[task setArguments: arguments];


I've tried the "arguments" portion in SOO many different ways.. I continue to get this error after execution of program:

Code:
Segmentation fault

Any help will be appreciated! :thumbup:
 
I'm no Objective-C guru, but does this page help?
http://www.cocoadev.com/index.pl?NSTask

I haven't done much command-line programming with Objective-C, mainly focusing with Cocoa/Cocoa Touch. But I hear ya on using strings in Objective-C. It can be weird to work with at first. I'll usually use Python for command-line stuff (both on Mac and PC) as it's a much more forgiving language and handles strings especially well.
 
Hey, thanks for the response.

I was looking at that site before, Objective-C make things more complex than they need to be lol at least in console programming. String manipulation its hell!

all i wanted to accomplish was something similar to this:

string test1;
string test2;
string answer;

answer = test1 + test2;

Yeah.. You gotta go through hell to make that happen in Objective-C :)
 
Yeah, there isn't a simple concatenate operator like "+" or "." unfortunately.

You could make your own function that emulates it, I imagine.

Given that, there are many ways of manipulating strings, using NSStrings by initially formatting them, or by appending strings to them. Or you can use NSMutableString to keep appending different strings to different strings.

Some ways I do it are pasted below, depending on situation like if I just need to add a one string to another, or if I have to add multiple strings, or finally if I have to keep altering (changing) a string with new additions.

Code:
// Some ways of appending or concatenating strings
	
	// stringWithFormat with multiple strings
	NSString *test1 = @"a";
	NSString *test2 = @"b";
	NSString *test3 = @"c";
	NSString *answer = [NSString stringWithFormat:@"%@%@%@", test1, test2, test3];
	NSLog(@"answer: %@", answer);
	
	// adding two strings with stringByAppendingString
	NSString *answer2 = [test1 stringByAppendingString: test2];
	NSLog(@"answer2: %@", answer2);
	
	// using NSMutableString and stringWithString
	// You can keep altering (adding, slicing, etc) a NSMutableString
	NSMutableString *answer3;
	answer3 = [NSMutableString stringWithString:test1];
	[answer3 appendString: test2];
	[answer3 appendString: test3];
	[answer3 appendString: @",etc"];
	
	NSLog(@"answer3: %@", answer3);

There are a lot more ways too. The "Programming in Objective-C 2.0" by Stephen G. Kochan has a great chapter just on strings.

This discussion on StackOverFlow about concatenating strings in Objective-C has some novel solutions, including writing you own concatenating function to make it easier.

http://stackoverflow.com/questions/5102 ... bjective-c

Then there is the issue of memory management, when to use "alloc" or not to use it. Which strings will get autoreleased and when you have to explicitly "retain" it to keep it from getting released.

This thread "differences between "[NSString alloc] initWithString...." and @"..." mentions this:
http://groups.google.com/group/iphone-a ... 19fd?pli=1

This is important especially when writing iPhone apps as there is no garbage collector so you'll have to rely on the retain/release to cleanup your objects. Mac OSX does have a garbage collector that you can enable however.

Definitely one of the challenges, memory management, when learning Objective-C when coming from a scripting language like PHP or ActionScript. Heck even from Java and C# too as I didn't have to worry about memory issues as much (but leaks do happen there too).

But yeah I agree. String manipulations requires you to go through more hoops than other languages.
 
Awesome Reply, thank you very much! I'm fairly new to Objective-C, i'm still not familiar with Objective-C's syntax and ways of doing things :) but this will come in VERY handy as i go down the road of learning the language.

Thanks again flyingturtle. :thumbup:
 
i'm new to programming too.i start to learn objective-c and reading kochan's book.i like to share ideas about learning with others who have experiences.
 
to answer the original question:
Code:
makehybrid -o ImageTest.iso /Users/Intrus/Desktop/newiso/ -iso -hfs -joliet -eltorito-boot newiso/cdboot -no-emul-boot -hfs-volume-name "TestDescription" -joliet-volume-name "TestDescription"
becomes (in the simplest form):
Code:
NSError *err;
    NSURL *desktop = [[NSFileManager defaultManager] URLForDirectory:NSDesktopDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:false error:&err];
    if (err != nil) {
        [[NSAlert alertWithError:err] runModal];
        return;
    }
    NSString *path = [desktop.path stringByAppendingPathComponent:@"newiso"];
    NSString *cdboot = [path stringByAppendingPathComponent:@"cdboot"];
    NSString *name = @"TestDescription";
    NSString *destination = [desktop.path stringByAppendingPathComponent:@"ImageTest.iso"];
    NSTask *hybridTask = [NSTask launchedTaskWithLaunchPath:@"/usr/bin/hdiutil" arguments:@[@"makehybrid", @"-o", destination, path, @"-iso", @"-hfs", @"-joliet", @"-eltorito-boot", cdboot, @"-no-emul-boot", @"-hfs-volume-name", name, @"-joliet-volume-name", name]];
of course standard cautions apply: make sure the name you assign is a valid volume name for both joliet and HFS+, and that you standardize the paths you will use, consider temporary directories, and make sure all errors are dealt with somehow. I haven't included any termination status information, or stdOut reading, but make sure you research those as well.
 
Status
Not open for further replies.
Back
Top