Turinys

Cocoa

Cocoa

NSLog()

NSLog(@"The number at index %d is %@", i, numberToPrint);

Galimi token'ai

Symbol Displays
%@ id
%d, %D, %i long
%u, %U unsigned long
%hi short
%hu unsigned short
%qi long long
%qu unsigned long long
%x, %X unsigned long printed as hexadecimal
%o, %O unsigned long printed as octal
%f, %e, %E, %g, %G double
%c unsigned char as ASCII character
%C unichar as Unicode character
%s char * (a null-terminated C string of ASCII characters)
%S unichar * (a null-terminated C string of Unicode characters)
%p void * (an address printed in hexadecimal with a leading 0x)
%% A % character

NSString

NSString *temp = @"this is a constant string";

C/Cocoa strings

// C string
char *foo;
// NSString
NSString *bar;
foo = "this is a C string";
bar = @"this is an NSString";

Kovertavimas

const char *foo = "Blah blah";
NSString *bar;
// Create an NSString from a C string
bar = [NSString stringWithUTF8String:foo];
 
// Create a C string from an NSString
foo = [bar UTF8String];

Metodai

int x = 5;
char *y = "abc";
id z = @"123";
NSString *aString = [[NSString alloc] initWithFormat:
            @"The int %d, the C String %s, and the NSString %@",
            x, y, z];

NSObject

- (id)init

Initializes the receiver after memory for it has been allocated. An init message is generally coupled with an alloc message in the same line of code:

TheClass *newObject = [[TheClass alloc] init];

- (NSString *)description

Returns an NSString that describes the receiver. The debugger's print object command („po“) invokes this method. A good description method will often make debugging easier. Also, if you use %@ in a format string, the object that should be substituted in is sent the message description. The value returned by the description method is put into the log string. For example, in your main function, the line

NSLog(@"The number at index %d is %@", i, numberToPrint);

is equivalent to

NSLog(@"The number at index %d is %@", i,
                            [numberToPrint description]);

- (BOOL)isEqual:(id)anObject

Returns YES if the receiver and anObject are equal and NO otherwise. You might use it like this:

if ([myObject isEqual:anotherObject]) {
    NSLog(@"They are equal.");
}

But what does equal really mean? In NSObject, this method is defined to return YES if and only if the receiver and anObject are the same object—that is, if both are pointers to the same memory location.

Clearly, this is not always the equal that you would hope for, so this method is overridden by many classes to implement a more appropriate idea of equality. For example, NSString overrides the method to compare the characters in the receiver and anObject. If they have the same characters in the same order, the two strings are considered equal.

Thus, if x and y are NSStrings, there is a big difference between these two expressions: x == y and [x isEqual:y]. The first expression compares the two pointers. The second expression compares the characters in the strings. Note, however, that if x and y are instances of a class that has not overridden NSObject's isEqual: method, the two expressions are equivalent.

NSArray

Loop'as

for (LotteryEntry *entryToPrint in array) {
    // Display its contents
    NSLog(@"%@", entryToPrint);
}

Metodai

NSMutableArray

Metodai

NSCalendarDate

Metodai

NSCalendarDate *now;
now = [NSCalendarDate calendarDate];
NSTimeZone *pacific = [NSTimeZone timeZoneWithName:@"PST"]
 
NSCalendarDate *hotTime = [NSCalendarDate dateWithYear:2000
                                            month:8
                                              day:3
                                             hour:16
                                           minute:0
                                           second:0
                                              timeZone:pacific];
NSCalendarDate *coldTime = [hotTime dateByAddingYears:0
                                               months:6
                                                 days:0
                                                hours:0
                                              minutes:0
                                              seconds:0];
Symbol Meaning
%y Year without century (00–99)
%Y Year with century („1990“)
%b Abbreviated month name („Jan“)
%B Full month name („January“)
%m Month as a decimal number (01–12)
%a Abbreviated weekday name („Fri“)
%A Full weekday name („Friday“)
%w Weekday as a decimal number (0–6), where Sunday is 0
%d Day of the month as a decimal number (01–31)
%e Same as %d but does not print the leading 0
%j Day of the year as a decimal number (001–366)
%H Hour based on a 24-hour clock as a decimal number (00–23)
%I Hour based on a 12-hour clock as a decimal number (01–12)
%p A.M./P.M. designation for the locale
%M Minute as a decimal number (00–59)
%S Second as a decimal number (00–59)
%F Milliseconds as a decimal number (000–999)
%x Date using the date representation for the locale
%X Time using the time representation for the locale
%c Shorthand for %X %x, the locale format for date and time
%Z Time zone name
%z Time zone offset in hours and minutes from GMT (HHMM)
%% A % character