tags:

views:

32

answers:

3

I need to pass a string as an argument but I dont know how..Help?

-(void)sendSMS: (int) number:(NSString)carrier;

That says objects cant be used as parameters.

+4  A: 

You should be using NSString* (notice the *) - what you want to be passing around is a pointer to an NSString object.

Try this (this naming convention is also much more Objective-c like):

-(void)sendSMStoNumber:(int)number withCarrier:(NSString*)carrier;

[myObject sendSMStoNumber:3 withCarrier:@"AT&T"];

Side Note, I'd recommend having your number variable be an NSString* as well, 10 digit numbers being what you're probably passing for a phone number and all, but I really don't know anything about what you're implementing and how.

BarrettJ
cool thanks. I'll change the int to NSString. But why do you have withCarrier: and toNumber: there. Does that name the parameter because I thought whats after the data type declaration was the name?
It's actually naming the selector (method) itself. The parameter you'll use inside the method body is still named the same. If you look at my example of calling the selector, it's clear what each parameter I'm passing is doing ("I'm sending an SMS to the number P with the carrier Q"). It's one of the ways Objective-C is different from other languages (you might notice Apple's APIs follow this naming convention as well).
BarrettJ
ah thats too cool! Your pretty helpful thanks!
+1  A: 

You were missing the pointer * in there:

-(void)sendSMS: (int) number:(NSString *)carrier;

cory.m.smith
A: 

That method is screwed up in a few ways. First, you can't pass a plain NSString — objects are always referenced through pointers. You also don't have a name for the first parameter, and the carrier argument is labeled as number:. I think you probably meant something like - (void)sendSMS:(NSInteger)number carrier:(NSString *)carrier.

(But even that is not really ideal if the number argument is supposed to represent a phone number. Phone numbers can start with a zero, while integers cannot.)

Chuck