My Blog List

Showing posts with label XCODE. Show all posts
Showing posts with label XCODE. Show all posts

Tuesday, June 7, 2011

Coding Study Notes:Foundation- NSNumber

Coding Study Notes:Foundation- NSNumber

Page 322 in the Kochan, Programming in Objective-C 2.0

·   create objects so that values declared as  C data types can be worked with as objects
·   Use NSNUmber class to create objects from different data types
·   Early example with integer value uses two lines, but later examples do conversion with one line of code, embedding the message/receiver syntax in along with the assignment statement

·   program 15.1 from the text


// Working with Numbers

//#import <Foundation/Foundation.h>
#import <Foundation/NSObject.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSValue.h>
#import <Foundation/NSString.h>


int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSNumber    *myNumber, *floatNumber, *intNumber;
    NSInteger   myInt; // why doesn't myInt need to be a pointer? Because it's used for conversion?
   
    intNumber = [NSNumber numberWithInteger: 100];
       
    // oh but wait, bet I can do the same thing in NSLog as I do with the other guys
    NSLog (@" Let's try to put message receiver into NSLog %li", [intNumber integerValue]);
   
    myInt = [intNumber integerValue]; //  for some reason intNumber has to get converted using integerValue

   
    NSLog (@"\n * * * *");
    NSLog (@"\n Before conversion The integer object was %li", intNumber );
   
    //I think we are casting the interger value as a long
    NSLog (@" %li", (long) myInt);
   
    // * * * long value
   
    myNumber = [NSNumber numberWithLong: 0xabcdef];
    NSLog (@"\n A long value %1x", [myNumber longValue]);
   
    // * * * * char value
   
    myNumber = [NSNumber numberWithChar: 'X'];
    NSLog (@"\n A character value %c", [myNumber charValue]);
   
    //float value
   
    floatNumber = [NSNumber numberWithFloat:100.00];
    NSLog (@"\n A float number %g", [floatNumber floatValue]);
   
    //double
   
    myNumber = [NSNumber numberWithDouble:12345e+15];
    NSLog (@"%1g", [myNumber doubleValue]);
   
    // wrong access
   
    NSLog (@"%i", [myNumber integerValue]); // no error, I have to know what I'm doing uh oh
   
    // Test two numbers for equality
   
    if ([intNumber isEqualToNumber: floatNumber]== YES)
    //    NSLog (@"Numbers are equal %li, %g," [intNumber integerValue] [floatNumber floatValue]);
    //    NSLog (@"Numbers are equal %i" , intNumber  ); runs clean - prints address of pointer
    {
    NSLog (@"Numbers are equal " );
        // page 325
        // NSLog format is [ my-NSNumber-declared-object retrieval-instance-method-for-NSNumber]
    NSLog (@" \n this integer %li", [intNumber integerValue]);
    NSLog (@" \n and a float I'm gonna show next " );
        NSLog (@"\n This float number %g", [floatNumber floatValue]);
       

    //&&&&&
    NSLog (@" \n %li", (long) myInt);
    }
    else
         NSLog (@"Numbers are not equal");
         
         // Test if one Number is <, ==, or > second number
         
    if ([intNumber compare: myNumber] == NSOrderedAscending)
         NSLog (@"First number is less than second");
   
    NSLog (@"First number is gonna print, then Second number \n");
    NSLog (@" \n this integer %li", [intNumber integerValue]);
    NSLog (@"%1g", [myNumber doubleValue]);
   
   
   
    [pool drain];
    return 0;
}

/*
 run
 [Switching to process 5458]
 Running…
 2011-06-06 22:02:40.622 NumberObjectsVer2[5458:a0f]  Let's try to put message receiver into NSLog 100
 2011-06-06 22:02:40.626 NumberObjectsVer2[5458:a0f]
 * * * *
 2011-06-06 22:02:40.626 NumberObjectsVer2[5458:a0f]
 Before conversion The integer object was 4296052080
 2011-06-06 22:02:40.627 NumberObjectsVer2[5458:a0f]  100
 2011-06-06 22:02:40.627 NumberObjectsVer2[5458:a0f]
 A long value abcdef
 2011-06-06 22:02:40.628 NumberObjectsVer2[5458:a0f]
 A character value X
 2011-06-06 22:02:40.629 NumberObjectsVer2[5458:a0f]
 A float number 100
 2011-06-06 22:02:40.629 NumberObjectsVer2[5458:a0f] 1.2345e+19
 2011-06-06 22:02:40.630 NumberObjectsVer2[5458:a0f] 0
 2011-06-06 22:02:40.630 NumberObjectsVer2[5458:a0f] Numbers are equal
 2011-06-06 22:02:40.631 NumberObjectsVer2[5458:a0f] 
 this integer 100
 2011-06-06 22:02:40.631 NumberObjectsVer2[5458:a0f] 
 and a float I'm gonna show next
 2011-06-06 22:02:40.632 NumberObjectsVer2[5458:a0f]
 This float number 100
 2011-06-06 22:02:40.632 NumberObjectsVer2[5458:a0f] 
 100
 2011-06-06 22:02:40.633 NumberObjectsVer2[5458:a0f] First number is less than second
 2011-06-06 22:02:40.633 NumberObjectsVer2[5458:a0f] First number is gonna print, then Second number
 2011-06-06 22:02:40.634 NumberObjectsVer2[5458:a0f] 
 this integer 100
 2011-06-06 22:02:40.634 NumberObjectsVer2[5458:a0f] 1.2345e+19
 Debugger stopped.
 Program exited with status value:0.
 */

Monday, June 6, 2011

Exploring Apple Documentation: Navigation






I need to work on really using the apple IOS documentation more. Though I've been looking at class reference pages, I haven't really tried to use the documentation to learn concepts.

This seems like a good place to start


http://developer.apple.com/library/ios/navigation/

It includes

*iOS Overview
*Tools for iOS Development
* A Short Practical Guide to Blocks
* Learning Objective-C: A Primer
* Creating an iPhone Application

Thursday, November 11, 2010

Debugging: Hey bobcat, to catch a fish, you gotta think like a fish


 
There was a kid’s book I had, when I still was a kid, about a native American child. Her elders told her that to catch a fish, she had to think like a fish. Or maybe it was bear, or a mountain goat. I don’t actually remember. So Little Bobcat developed her analytical little wits and finally caught herself a fish.

There’s a lot I have yet to learn about the XCODE debugger. In fact, just about everything.  I haven’t even figured out where to go to learn about how the debugger works. I’m still just looking at error messages and going from there. Tonight I had ‘duplicate symbol _main’. I clicked on every highlighted place I could find. I looked at the thread displaying the hierarchy of file names. I looked through each of the five files I’d created, but I only found the word ‘main’ in one place. * Finally I just decided to think like a fish.

Somewhere, I must have more than one of those lines that looks like this

int main(void) {

but I sure didn’t see it in any of my ‘.m’ or ‘.h’ files.

So I started opening up gates. That’s what a fish would do, right? A fish like XCODE looks at all the files in the project. (One heck of a smart fish) and that fish recognizes the name of the project. So the fact that I was expecting my fish to only respond to the lines  (my fishing line that is) in a file called ‘main.m’, didn’t mean that she wasn’t going to look around for other files. Particularly a file with the same name as my project – ‘protocols.m’, a file created by default in a folder called ‘SOURCE’ when I created a new project. Sure enough, there was the culprit line. More than one ‘main’ makes trouble.

Cast your (main) lines upon the water, but only one at a time.

* After I resolved this error,  it occurred to me that ‘find in project’ would have been another good way to scout out this problem.