Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cocoa programming question
Mars_999
Unregistered

 
Post: #1
Cocoa programming question
I am following the tutorial on the Currency Converter in the Learning Cocoa book. I am at the point where it says to "click the electrical outlet icon to the right of the class?" I don't see this icon anywhere in IB? I am using Dec 2002 build of PB. Thanks
2003.04.17 06:24 PM
Quote this message in a reply
swcrissman
Unregistered

 
Post: #2
Cocoa programming question
They changed the interface on this, obviously. I went back and looked at what it looked like before, from what I can see, you want to do the following:

- in the nib window, click on the classes tab
- click on the class name you are interested in (ConverterController if I'm looking where you are)
- bring up the inspector window (command-shift-i)
- from the inspector dropdown, select attributes.
- at the bottom of that window, there should be an outlets and an actions tab
- select the outlet tab, and click add

I'm sure you can figure the rest out.

Spencer
2003.04.17 06:42 PM
Quote this message in a reply
Mars_999
Unregistered

 
Post: #3
Cocoa programming question
Cool thanks for the tip. I finally got the example to work. Now about data sizes and how to get a different type other than float. Lets say I have a int type or a String how would I deal with that in ObjC?
Code:
-(IBAction)convert:(id)sender
{
float rate;
char *cp = new char[255];
int number;

rate = [dollarField floatValue];
cp = [nameField ??];
number = [numberField ??intValue];
}

I am only guessing at the int type but have no idea on the char pointer? I don't know how ObjC and Cocoa deal with strings or if you can even use a C style string to recieve data from the user? Thanks and so far Cocoa is quite cool. Way less code to get a GUI up and running and to take user input.

Ah dont' mind the unhappy emoticon
2003.04.17 08:52 PM
Quote this message in a reply
OneSadCookie Offline
Genius Bar
*****

Posts: 2,154
Joined: Feb 2005
Post: #4
Cocoa programming question
You can send -intValue, -floatValue, -stringValue, &c to a field to get its content in various forms. -stringValue returns you an NSString*. If you really need a const char* from an NSString* (which you shouldn't), you can send the string a -cString message (fails if the contents of the string can't be represented in MacRoman), a -lossyCString message (puts ?s in if the character can't be converted), or a -UTF8String (preserves all characters but non-ascii characters use more than one [font=monaco,courier]char[/font]).

Learn to use the documentation, that way you won't have to make me use it to answer your questions Smile
2003.04.17 10:37 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Criollo
Unregistered

 
Post: #5
Cocoa programming question
Quote:I don't know how ObjC and Cocoa deal with strings or if you can even use a C style string to recieve data from the user?

Objective-C is just a superset of C, so anything you can do with C, you can do in Obj-C, including using C style strings. Most objects and methods in Cocoa take NSStrings as arguments though, so you will probably want to use NSStrings in your Cocoa applications. To create an NSString from a C string, use +stringWithCString and to convert an NSString to a C string, send the -cString message.

Quote:Learn to use the documentation, that way you won't have to make me use it to answer your questions

I think this is mentioned in the appendix of the Learning Cocoa book, but if you need a quick and easy way to look through the Cocoa doccumentation, you should check out the Cocoa Browser. You can find it at:

http://homepage2.nifty.com/hoshi-takanor...a-browser/

Hope that helps.

-Criollo
2003.04.18 03:22 AM
Quote this message in a reply
swcrissman
Unregistered

 
Post: #6
Cocoa programming question
Quote:Originally posted by Mars_999
Code:
-(IBAction)convert:(id)sender
{
float rate;
char *cp = new char[255];
int number;

rate = [dollarField floatValue];
cp = [nameField ??];
number = [numberField ??intValue];
}


I don't know what level you are at, so if the others weren't clear your code would become more like this:
Code:
-(IBAction)convert:(id)sender
{
   float     rate;
   NSString* name;
   int       number;

   rate   = [dollarField floatValue];
   name   = [nameField stringValue];
   number = [numberField intValue];

   // If you really need a char*
   char* namec = [name cString];
}

You'll need to learn NSString pretty well to use most of the Cocoa framework.


Hope that helps.

Spencer
2003.04.18 09:59 AM
Quote this message in a reply
Mars_999
Unregistered

 
Post: #7
Cocoa programming question
Quote:Originally posted by swcrissman
I don't know what level you are at, so if the others weren't clear your code would become more like this:
Code:
-(IBAction)convert:(id)sender
{
   float     rate;
   NSString* name;
   int       number;

   rate   = [dollarField floatValue];
   name   = [nameField stringValue];
   number = [numberField intValue];

   // If you really need a char*
   char* namec = [name cString];
}

You'll need to learn NSString pretty well to use most of the Cocoa framework.


Hope that helps.

Spencer


Well in C++ I would probably say novice? ObjC NOOB. The issue with using NSSring's is this. I am going to save the string data into a file along with other data using fstream class in C++. Will their be a problem saving a NSString type with ofstream? How about when I go to load back in my data? How am I going to know how much to read in? With a char *cp = new char[255] I need to read in 255 bytes. So if saving and loading a NSString is going to be a no no, I will have to convert the NSStrings to C sytle strings and save them. I have the int, float data types saving ok with ofstream. My other question is does ObjC have a sizeof() operator? If not is int, float, 4bytes; double 8bytes; short 2bytes? I know I should buy a ObjC book but not sure what to think of it. Want to play around with it first and if I like it I will buy a book. I have 4 books on C++ and one on C. And many on OpenGL. So I don't have a problem buying books for subjects I like. What is a good recommendation for a ObjC book? Will that book have Cocoa example also? thanks all for your help.
2003.04.18 08:23 PM
Quote this message in a reply
OneSadCookie Offline
Genius Bar
*****

Posts: 2,154
Joined: Feb 2005
Post: #8
Cocoa programming question
Objective C is a superset of C, so anything you can do in C you can do in Objective C.

If you insist on writing NSStrings to ofstreams, try something like this:

Code:
const char* utf8String = [myNSString UTF8String];
UInt32 byteCount = strlen(utf8String);
myOFStream.write(&byteCount, sizeof(UInt32));
myOFStream.write(utf8String, byteCount);

Code:
UInt32 byteCount;
myIFStream.read(&byteCount, sizeof(UInt32));
char* buffer = new char[byteCount + 1];
myIFStream.read(buffer, byteCount);
buffer[byteCount] = 0;
NSString* myNSString = [NSString stringWithUTF8String:buffer];
delete [] buffer;
2003.04.18 08:53 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Mars_999
Unregistered

 
Post: #9
Cocoa programming question
I have looked on Apples webpage for a way to take a C style string and convert it to a NSString and not finding anyway to do it? I see NSString to cString but not the other way around?
2003.04.26 12:22 AM
Quote this message in a reply
OneSadCookie Offline
Genius Bar
*****

Posts: 2,154
Joined: Feb 2005
Post: #10
Cocoa programming question
+stringWithCString or +stringWithUTF8String
2003.04.26 04:06 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)