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

jamesapp

macrumors 6502a
Original poster
Mar 7, 2008
544
0
james-collinss-macbook-pro:prog15 jamescollins$ ./prog15.1
100
abcdef
X
100
nan
0
Numbers are equal
First number is less than second

got this output while i tried to compile and run a program from a book on objective-c by Stephen G. Kochan.

here is my program. which i called prog15.1.m

Code:
// working with numbers

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

#import <Foundation/NSString.h>
#import <stdio.h>

int main (int argc, char *argv[])
{
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  NSNumber          *myNumber, *floatNumber, *intNumber;
  int               i;
  
  // integer value
  
  intNumber = [NSNumber numberWithInt: 100];
  printf ("%i\n", [intNumber intValue]);
  
  // long value
  
  myNumber = [NSNumber numberWithLong: 0xabcdef];
  printf ("%lx\n", [myNumber longValue]);
  
  // char value
  
  myNumber = [NSNumber numberWithChar: 'X'];
  printf ("%c\n", [myNumber charValue]);
  
  // float value
  
  floatNumber = [NSNumber numberWithFloat: 100.00];
  printf ("%g\n", [floatNumber floatValue]);
  
  // double
  
  myNumber = [NSNumber numberWithDouble: 12345e+15];
  printf ("%Lg\n", [myNumber intValue]);
  
  // wrong access here
  
  printf ("%i\n", [myNumber intValue]);
  
  // Test two Numbers for equality
  
  if ([intNumber isEqualToNumber: floatNumber] == YES)
         printf ("Numbers are equal\n");
  else
         printf ("Numbers are not equal\n");
         
  // Test if one Number is <, ==, or > second Number
  
  if ([intNumber compare: myNumber] == NSOrderedAscending)
         printf ("First number is less than second\n");
         
  [pool release];
  return 0;
}
[code\]

the output from the book is as follows.

100
abcdef
X
100
1.2345e+19
2147483647
Numbers are equal
First number is less than second

just wondering why my output doesen't match the book's.
 

jamesapp

macrumors 6502a
Original poster
Mar 7, 2008
544
0
program problem1

james-collinss-macbook-pro:prog15 jamescollins$ ./prog15.4
Length of str1: 16
2008-04-13 16:41:53.594 prog15.4[167:10b] *** +[NSString strinWithString:]: unrecognized selector sent to class 0xa0409f00
2008-04-13 16:41:53.596 prog15.4[167:10b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSString strinWithString:]: unrecognized selector sent to class 0xa0409f00'
2008-04-13 16:41:53.597 prog15.4[167:10b] Stack: (
2429948491,
2452074747,
2429977866,
2429971020,
2429971218
)
Trace/BPT trap

got this output from a book on programming
the program compiled to an executable but when i tried to run it i got the above
output. here is a file i called prog15.4


Code:
// basic string operations

#import <Foundation/NSObject.h>
#import <Foundation/NSString.h>
#import <Foundation/NSAutoreleasePool.h>
#import <stdio.h>
 
int main (int argc, char *argv[])
{
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  NSString *str1 = @"This is string A";
  NSString *str2 = @"This is string B";
  NSString *res;
  NSComparisonResult compareResult;
  
  // count the number of characters
  
  printf ("Length of str1: %i\n", [str1 length]);
  
  // copy one string to another
  
  res = [NSString strinWithString: str1];
  printf ("Copy: %s\n", [res cString]);
  
  // copy one string to the end of another
  
  str2 = [str1 stringByAppendingString: str2];
  printf ("Concatentation: %s\n", [str2 cString]);
  
  // test if 2 strings are equal
  
  if ([str1 isEqualToString: res] == YES)
        printf ("str1 == res\n");
  else
        printf ("str1 != res\n");
        
  // test if one string is <, == or > than another
  
  compareResult = [str1 compare: str2];
  
  if (compareResult == NSOrderedAscending)
       printf ("str1 < str2\n");
  else if (compareResult == NSOrderedSame)
       printf ("str1 == str2\n");
  else // must be NSOrderedDescending
       printf ("str1 > str2\n");
       
 // convert a string to uppercase
 
 res = [str1 uppercaseString];
 printf ("Uppercase conversion: %s\n", [res cString]);
 
 // convert a string to lowercase
  
  res = [str1 lowercaseString];
  printf ("Lowercase conversion: %s\n", [res cString]);
  
  printf ("Original string: %s\n", [str1 cString]);
  
  [pool release];
  return 0;
}

here is the output from the book

Length of string1: 16
Copy: This is string A
Concatentation: This is string AThis is string B
str1 == res
str1 < str2
Uppercase conversion: THIS IS STRING A
lowercase conversion: this is string a
Original string: This is string A
 

jamesapp

macrumors 6502a
Original poster
Mar 7, 2008
544
0
program problem2

prog15.5.m:19: warning: ‘cString’ is deprecated (declared at /System/Library/Frameworks/Foundation.framework/Headers/NSString.h:345)
prog15.5.m:24: warning: ‘cString’ is deprecated (declared at /System/Library/Frameworks/Foundation.framework/Headers/NSString.h:345)
prog15.5.m:29: warning: ‘cString’ is deprecated (declared at /System/Library/Frameworks/Foundation.framework/Headers/NSString.h:345)
prog15.5.m:34: warning: ‘cString’ is deprecated (declared at /System/Library/Frameworks/Foundation.framework/Headers/NSString.h:345)

got these warning messages from a program from a book
here is my test program which i called prog15.5.m

Code:
// basic string operations

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

int main (int argc, char *argv[])
{
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  NSString *str1 = @"This is string A";
  NSString *str2 = @"This is string B";
  NSString *res;
  NSRange subRange;
  
  // extract first 4 chars from string
  
  res = [str1 substringToIndex: 3];
  printf ("First 3 chars of str1: %s\n", [res cString]);
  
  // extract chars to end of string starting at index 5
  
  res = [str1 substringFromIndex: 5];
  printf ("Chars from index 5 of str1: %s\n", [res cString]);
  
  // extract chars from index 8 through 13 (6 chars)
  
  res = [[str1 substringFromIndex: 8] substringToIndex: 6];
  printf ("Chars from index 8 through 13: %s\n", [res cString]);
  
  // an easier way to do the same thing
  
  res = [str1 substringWithRange: NSMakeRange (8, 6)];
  printf ("Chars from index 8 through 13: %s\n", [res cString]);
  
  // locate one string inside another
  
  subRange = [str1 rangeOfString: @"string A"];
  printf ("String is at index %i, length is %i\n",
                subRange.location, subRange.length);
                
  subRange = [str1 rangeOfString: @"string B"];
  
  if (subRange.location == NSNotFound)
        printf ("String not found\n");
  else
        printf ("String is at index %i, length is %i\n",
               subRange.location, subRange.length);
               
  [pool release];
  return 0;
}
someone told me to treat warning messages as error messages unless you know otherwise. just wondering what these warning messages are ?
 

jamesapp

macrumors 6502a
Original poster
Mar 7, 2008
544
0
program problem3

james-collinss-macbook-pro:prog15 jamescollins$ gcc -framework Foundation prog15.6.m -o prog15.6
prog15.6.m: In function ‘main’:
prog15.6.m:19: warning: ‘cString’ is deprecated (declared at /System/Library/Frameworks/Foundation.framework/Headers/NSString.h:345)
prog15.6.m:23: warning: ‘NSMutableString’ may not respond to ‘-inserString:atIndex:’
prog15.6.m:23: warning: (Messages without a matching method signature
prog15.6.m:23: warning: will be assumed to return ‘id’ and accept
prog15.6.m:23: warning: ‘...’ as arguments.)
prog15.6.m:24: warning: ‘cString’ is deprecated (declared at /System/Library/Frameworks/Foundation.framework/Headers/NSString.h:345)
prog15.6.m:28: warning: ‘NSMutableString’ may not respond to ‘-inserString:atIndex:’
prog15.6.m:29: warning: ‘cString’ is deprecated (declared at /System/Library/Frameworks/Foundation.framework/Headers/NSString.h:345)
prog15.6.m:34: warning: ‘cString’ is deprecated (declared at /System/Library/Frameworks/Foundation.framework/Headers/NSString.h:345)
prog15.6.m:39: warning: ‘cString’ is deprecated (declared at /System/Library/Frameworks/Foundation.framework/Headers/NSString.h:345)
prog15.6.m:47: warning: ‘cString’ is deprecated (declared at /System/Library/Frameworks/Foundation.framework/Headers/NSString.h:345)
prog15.6.m:53: warning: ‘cString’ is deprecated (declared at /System/Library/Frameworks/Foundation.framework/Headers/NSString.h:345)
prog15.6.m:59: warning: ‘cString’ is deprecated (declared at /System/Library/Frameworks/Foundation.framework/Headers/NSString.h:345)
prog15.6.m:71: warning: ‘cString’ is deprecated (declared at /System/Library/Frameworks/Foundation.framework/Headers/NSString.h:345)
prog15.6.m:87: warning: ‘cString’ is deprecated (declared at /System/Library/Frameworks/Foundation.framework/Headers/NSString.h:345)

got this from a program i tried to compile
the program compiled but when i went to run it i got this output

james-collinss-macbook-pro:prog15 jamescollins$ ./prog15.6
This is string A
2008-04-13 22:09:53.821 prog15.6[292:10b] *** -[NSCFString inserString:atIndex:]: unrecognized selector sent to instance 0x105710
2008-04-13 22:09:53.825 prog15.6[292:10b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFString inserString:atIndex:]: unrecognized selector sent to instance 0x105710'
2008-04-13 22:09:53.826 prog15.6[292:10b] Stack: (
2429948491,
2452074747,
2429977674,
2429971020,
2429971218
)
Trace/BPT trap

here is the program which i called prog15.m

Code:
// basic string operations - mutable strings

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

int main (int argc, char *argv[])
{
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  NSString          *str1 =@"This is string A";
  NSString          *search, *replace;
  NSMutableString   *mstr;
  NSRange           substr;
  
  // create mutable string from immutable
  
  mstr = [NSMutableString stringWithString: str1];
  printf ("%s\n", [mstr cString]);
  
  // insert characters starting at a specific index
  
  [mstr inserString: @" mutable" atIndex: 7];
  printf ("%s\n", [mstr cString]);
  
  // effective concatentation if insert at end
  
  [mstr inserString: @" and string B" atIndex: [mstr length]];
  printf ("%s\n", [mstr cString]);
  
  // or can use appendString directly
  
  [mstr appendString: @" and string C"];
  printf ("%s\n", [mstr cString]);
  
  // delete substring based on range
  
  [mstr deleteCharactersInRange: NSMakeRange (16, 13)];
  printf ("%s\n", [mstr cString]);
  
  // find range first and then use it for deletion
  
  substr = [mstr rangeOfString: @"string B and "];
  
  if (substr.location != NSNotFound) {
  [mstr deleteCharactersInRange: substr];
  printf ("%s\n", [mstr cString]);
}

// set the mutable string directly

  [mstr setString: @"This is string A"];
  printf ("%s\n", [mstr cString]);
  
  // now let's replace a range of chars with another
  
  [mstr replaceCharactersInRange: NSMakeRange(8, 8)
        withString: @"a mutable string"];
  printf ("%s\n", [mstr cString]);
  
  // search and replace
  
  search = @"This is";
  replace = @"An example of";
  
  substr = [mstr rangeOfString: search];
  
  if (substr.location != NSNotFound) {
        [mstr replaceCharactersInRange: substr
              withString: replace];
          printf ("%s\n", [mstr cString]);
  }
  
  // search and replace all occurrences
  
  search = @"a";
  replace = @"X";
  
  substr = [mstr rangeOfString: search];
  
  while (substr.location != NSNotFound) {
        [mstr replaceCharactersInRange: substr
              withString: replace];
         substr = [mstr rangeOfString: search];
  }
  
  printf ("%s\n", [mstr cString]);
  
  [pool release];
  return 0;
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.