PDA

View Full Version : newbie question: How to get input from a user for an object such as NSString




kingthong
Sep 23, 2010, 04:03 AM
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

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
Sep 23, 2010, 04:23 AM
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

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.


//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
Sep 23, 2010, 04:50 AM
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
Sep 23, 2010, 05:00 AM
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.


NSString *myString = nil;

myString = [textBox stringValue];

kingthong
Sep 23, 2010, 05:10 AM
oh brilliant! i get it. right now i'm using GCC mainly to get the feel of Obj-C
Thanks MorphingDragon!

MareoRaft
Mar 23, 2013, 03:00 PM
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
Mar 23, 2013, 03:23 PM
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
Mar 23, 2013, 04:47 PM
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
Mar 23, 2013, 04:52 PM
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
Mar 24, 2013, 11:38 AM
I would vote for Xcode, the IDE, because it is similar to Delphi and C++Builder in the past.

Instead of

myString = [textBox stringValue];

I remember having used

myString = self.textBox.stringValue;

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

ArtOfWarfare
Mar 24, 2013, 01:29 PM
I would vote for Xcode, the IDE, because it is similar to Delphi and C++Builder in the past.

Instead of

myString = [textBox stringValue];

I remember having used

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:
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:

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

Or am I mistaken?

gnasher729
Mar 24, 2013, 01:39 PM
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:

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

self.count++

will be compiled as

(tmp = self.count, self.count = tmp + 1, tmp)