Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

kingthong

macrumors member
Original poster
Sep 20, 2010
62
0
Somewhere but not here.
Hi,

it might be a silly question but i've just started learning Objective-C.
My question is how do you get input from a user after initializing an object of NSString type.

i.e say i've initialized

Code:
NSString *name;
name = [[NSString alloc] init] ;

i want the user to enter the value for Name. can i use a scanf?
I've tried googling but to no avail.

Thanks!
 

MorphingDragon

macrumors 603
Mar 27, 2009
5,160
6
The World Inbetween
Hi,

it might be a silly question but i've just started learning Objective-C.
My question is how do you get input from a user after initializing an object of NSString type.

i.e say i've initialized

Code:
NSString *name;
name = [[NSString alloc] init] ;


i want the user to enter the value for Name. can i use a scanf?
I've tried googling but to no avail.

Thanks!

NSString has a method called stringWithCString.

So you can use scanf to get a C string then use that method.

Code:
//Garbage collection or a pool must be present

//Declare Variables
char cString[100]; //cString is a string of 100 characters (technically an array of 100 characters)
NSString *myString;
	
//Get the string form the user
NSLog(@"Enter a string...");
scanf("%s", &cString);

//Convert to NSString
myString = [NSString stringWithCString: cString encoding: NSASCIIStringEncoding];

//Log Result
NSLog(@"You entered: %@", myString);
 

kingthong

macrumors member
Original poster
Sep 20, 2010
62
0
Somewhere but not here.
Thanks for the prompt reply!

One more question as an addendum-

1. Is this the only/most efficient way to read an input from a user? I will be coding on an IPhone eventually so memory and efficiency will be vital.

Thanks again.
 

MorphingDragon

macrumors 603
Mar 27, 2009
5,160
6
The World Inbetween
Thanks for the prompt reply!

One more question as an addendum-

1. Is this the only/most efficient way to read an input from a user? I will be coding on an IPhone eventually so memory and efficiency will be vital.

Thanks again.

scanf will only be needed during the learning process and when coding command line based utilities.

When you start making GUI based applications, getting a string from a user will be as simple as getting the data from a text box.

Code:
NSString *myString = nil;

myString = [textBox stringValue];
 

MareoRaft

macrumors newbie
Oct 20, 2005
16
1
When I do this, I get the error "warning: passing argument 1 of 'stringWithCString:encoding:' makes pointer from integer without a cast"

Is there some #import statement I should be including?
 

chown33

Moderator
Staff member
Aug 9, 2009
10,751
8,425
A sea of green
When I do this, I get the error "warning: passing argument 1 of 'stringWithCString:encoding:' makes pointer from integer without a cast"

Is there some #import statement I should be including?

Post your actual code, exactly as you compiled it, and identify exactly where the error message occurs.
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
When I do this, I get the error "warning: passing argument 1 of 'stringWithCString:encoding:' makes pointer from integer without a cast"

Is there some #import statement I should be including?

I recommend using Xcode and the Clang compiler, not gcc. You will get much better error messages.
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,563
6,062
I recommend using Xcode and the Clang compiler, not gcc. You will get much better error messages.

I will back Clang, but not Xcode at this point. Try to avoid learning too many things at once. Obj-C is a language, Clang / GCC are both compilers, and Xcode is an IDE. All of them are interchangeable components that make up programming.
 

Hans Kamp

macrumors member
Mar 24, 2013
38
0
Enschede, Netherlands
I would vote for Xcode, the IDE, because it is similar to Delphi and C++Builder in the past.

Instead of

Code:
myString = [textBox stringValue];

I remember having used

Code:
myString = self.textBox.stringValue;

I have to check it soon, whether I am right about this.
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,563
6,062
I would vote for Xcode, the IDE, because it is similar to Delphi and C++Builder in the past.

Instead of

Code:
myString = [textBox stringValue];

I remember having used

Code:
myString = self.textBox.stringValue;

I have to check it soon, whether I am right about this.

Dot notation on the right hand side of an assignment is the same as calling methods without arguments.

So these two lines are equivalent:
Code:
myString = [[self textBox] stringValue];
myString = self.textBox.stringValue;

Quick question... is it possible to write a setter function which returns the value set as use it with dot notation? It might allow something like:

Code:
myString = self.textBox1.stringValue = self.textBox2.stringValue;

Or am I mistaken?
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
Quick question... is it possible to write a setter function which returns the value set as use it with dot notation? It might allow something like:

Code:
myString = self.textBox1.stringValue = self.textBox2.stringValue;

Or am I mistaken?

I haven't tried it, but that should work anyway. The Objective-C compiler assumes you have setter and getter, and generates any other code you need. For example

Code:
   self.count++
will be compiled as

Code:
  (tmp = self.count, self.count = tmp + 1, tmp)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.