Cocoa

Cocoa

  • Beginning Cocoa programmers are often eager to create subclasses of NSString and NSMutableArray. Don't. Stylish Objective-C programmers almost never do.

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";
  • immutable (mutable - NSMutableString)

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

  • -(id)initWithFormat:(NSString *)format, … - Works like sprintf. Here, format is a string containing tokens, such as %d. The additional arguments are substituted for the tokens:
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];
  • - (unsigned int)length - Returns the number of characters in the receiver.
  • - (NSString *)stringByAppendingString:(NSString *)aString - Returns a string object made by appending aString to the receiver.

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

  • laiko pointer'ius į objektus
  • į jį negalima įdėti nil, tam suveiks NSNull
    • [myArray addObject:[NSNull null]];
  • sukūrus negalima pridėti/išimti objektų (skirtingai nei NSMutableArray)

Loop'as

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

Metodai

  • - (unsigned)count - Returns the number of objects currently in the array.
  • -(id)objectAtIndex:(unsigned)i - Returns the object located at index i. If i is beyond the end of the array, you will get an error at runtime.
  • -(id)lastObject - Returns the object in the array with the highest index value. If the array is empty, nil is returned.
  • -(BOOL)containsObject:(id)anObject - Returns YES if anObject is present in the array. This method determines whether an object is present in the array by sending an isEqual: message to each of the array's objects and passing anObject as the parameter.
  • -(unsigned)indexOfObject:(id)anObject - Searches the receiver for anObject and returns the lowest index whose corresponding array value is equal to anObject. Objects are considered equal if isEqual: returns YES. If none of the objects in the array are equal to anObject, indexOfObject: returns NSNotFound.

NSMutableArray

  • į tokį iš NSArray galima nukopijuoti naudojant metodą mutableCopy
  • į jį taip pat negalima įdėti nil (tam suveiks NSNull)
    • [myArray addObject:[NSNull null]];

Metodai

  • - (void)addObject:(id)anObject - Inserts anObject at the end of the receiver. You are not allowed to add nil to the array.
  • - (void)addObjectsFromArray:(NSArray *)otherArray - Adds the objects contained in otherArray to the end of the receiver's array of objects.
  • - (void)insertObject:(id)anObject atIndex:(unsigned)index - Inserts anObject into the receiver at index. If index is already occupied, the objects at index and beyond are shifted up one slot to make room. Since index cannot be greater than the number of elements in the array, you will get an error if anObject is nil or if index is greater than the number of elements in the array.
  • - (void)removeAllObjects - Empties the receiver of all its elements.
  • - (void)removeObject:(id)anObject - Removes all occurrences of anObject in the array. Matches are determined on the basis of anObject's response to the isEqual: message.
  • - (void)removeObjectAtIndex:(unsigned)index - Removes the object at index and moves all elements beyond index down one slot to fill the gap. You will get an error if index is beyond the end of the array.

NSCalendarDate

  • immutable, galima keisti tik formatą

Metodai

  • + (id)calendarDate - This method creates and returns a calendar date initialized to the current date and time in the default format for the locale. Klasės metodas, ne objekto (static?). Galima naudoti taip:
NSCalendarDate *now;
now = [NSCalendarDate calendarDate];
  • + (id)dateWithYear:(int)year month:(unsigned)month day:(unsigned)day hour:(unsigned)hour minute:(unsigned)minute second:(unsigned)second timeZone:(NSTimeZone *)aTimeZone - This class method returns an autoreleased object. Specifically, this class method creates and returns a calendar date initialized with the specified values.
NSTimeZone *pacific = [NSTimeZone timeZoneWithName:@"PST"]
 
NSCalendarDate *hotTime = [NSCalendarDate dateWithYear:2000
                                            month:8
                                              day:3
                                             hour:16
                                           minute:0
                                           second:0
                                              timeZone:pacific];
  • - (NSCalendarDate *)dateByAddingYears:(int)year months:(int)month days:(int)day hours:(int)hour minutes:(int)minute seconds:(int)second - This method returns a calendar date with the year, month, day, hour, minute, and second offsets specified as arguments. A positive offset is the future, and a negative offset represents the past.
NSCalendarDate *coldTime = [hotTime dateByAddingYears:0
                                               months:6
                                                 days:0
                                                hours:0
                                              minutes:0
                                              seconds:0];
  • - (int)dayOfCommonEra, - (int)dayOfMonth, - (int)dayOfWeek, - (int)dayOfYear, - (int)hourOfDay, - (int)minuteOfHour, - (int)monthOfYear
  • - (void)setCalendarFormat:(NSString *)format - This method sets the default calendar format for the receiver. A calendar format is a string formatted with date-conversion specifiers.
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
  • - (NSDate *)laterDate:(NSDate *)anotherDate - This method is inherited from NSDate, compares the receiver to anotherDate, and returns the later of the two.
  • - (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate - This method returns the interval in seconds between the receiver and anotherDate. If the receiver is earlier than anotherDate, the return value is negative. NSTimeInterval is the same as double.
This website uses cookies for visitor traffic analysis. By using the website, you agree with storing the cookies on your computer.More information
 
Jei nenurodyta kitaip, šio wiki turinys ginamas tokia licencija: CC Attribution-Noncommercial-Share Alike 4.0 International
Recent changes RSS feed Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki