Вы находитесь на странице: 1из 20

Objective-c


NSString .
:
+ stringWithFormat - ;
+ localizedStringWithFormat -
;
+ stringWithUTF8String - utf8 ;
length - ;
stringByAppendingString - ;
componentsSeparatedByString -
;
substringFromIndex - ;
substringWithRange - ;
substringToIndex - ;
hasPrefix - ;
hasSuffix - ;
isEqualToString - ;
compare - , enum:
NSOrderedAscending - ;
NSOrderedSame - ;
NSOrderedDescending - ;
caseInsensitiveCompare - ;
localizedCompare - ;
localizedCaseInsensitiveCompare -
,
capitalizedString - ;
lowercaseString - ;
uppercaseString - ;
doubleValue - double;
floatValue - float;
intValue - ;
integerValue - ;
longLongValue - ;
boolValue - , YES
"Y", "y", "T", "t" 1 9;

stringByAddingPercentEscapesUsingEncoding -
URL (%),

stringByReplacingPercentEscapesUsingEncoding;
stringByReplacingPercentEscapesUsingEncoding -
;
@"....".
NSString *str = @"Hello world";
NSString *str1 = [NSString stringWithFormat:@"%@ %@ ", s1, s2];
//
NSString *src = @"Hello world! - most known sentence";
NSRange range = [src rangeOfString:@"most"];
// , ?
if (range.length > 0)
NSLog(@"Range is: %@", NSStringFromRange(range));

,
NSMutableString:
appendFormat - ,
;
appendString - ;
deleteCharactersInRange - ;
insertString - ;
replaceCharactersInRange - ;
replaceOccurrencesOfString - ;
setString - ;


Cocoa :
NSArray - , ..

NSMutableArray - .
NSArray, ,
;
C.

+ array - , ;
+ arrayWithArray:- ,
;
+ arrayWithContentsOfFile: -

+ arrayWithContentsOfURL: - , xml
url;
+ arrayWithObject: - ;
+ arrayWithObjects: -
, nil;
+ arrayWithObjects:count: - -;
initWithArray: -
;
initWithArray:copyItems: - ,
,
NSCopying ;
initWithContentsOfFile: -
(. arrayWithContentsOfFile);
initWithContentsOfURL: -
url (. arrayWithContentsOfURL);
initWithObjects: -
, nil;
initWithObjects:count: -
C-;

containsObject: - ;
count - ;
getObjects:range: -
( - (id*));
lastObject - , nil ;
objectAtIndex: - ;
objectsAtIndexes: -
;
objectEnumerator -
;
reverseObjectEnumerator - ,
.. ;


isEqual: NSObject.
NSNotFound.
indexOfObject: - ;
indexOfObject:inRange: -
;
indexOfObjectPassingTest: - ,
;
indexOfObjectWithOptions:passingTest: - ,
;
indexOfObjectAtIndexes:options:passingTest: - ;
indexesOfObjectsPassingTest: -
, ;
indexesOfObjectsWithOptions:passingTest: - ;
indexesOfObjectsAtIndexes:options:passingTest: - ;

removeAllObjects - ;
removeLastObject - ;
removeObject: - ;
removeObject:inRange: - ;
removeObjectAtIndex: - ;
removeObjectsAtIndexes: -
;

removeObjectsInArray: - ;
removeObjectsInRange: - ;


:
NSDictionary - , ..

NSMutableDictionary - .
NSDictionary,
;
objective-c .
// ,
NSArray *keys = [[NSArray alloc]
@"key3", nil];
NSArray *objs = [[NSArray alloc]
@"obj3", nil];

initWithObjects:@"key1",

@"key2",

initWithObjects:@"obj1",

@"obj2",

//
NSDictionary
*dic
=
[[NSDictionary
forKeys:keys];

alloc]

initWithObjects:objs

//
for (id key in dic) {
NSLog(@"key: %@
value:%@", key, [dic objectForKey:key]);
}
!
//
[keys release];
[objs release];
[dic release];

+ (id)dictionaryWithObjectsAndKeys:(id)firstObject , ... -
/.
nil ;
+ (id)dictionaryWithObject:(id)anObject forKey:(id)aKey -
;
+ (id)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray *)keys ;
Creates and returns a dictionary containing count objects from the objects
array.

+ (id)dictionaryWithObjects:(id *)objects forKeys:(id *)keys count:


(NSUInteger)count -
C;
+ (id)dictionaryWithContentsOfFile:(NSString *)path -
;
+ (id)dictionaryWithContentsOfURL:(NSURL *)aURL -
url ;
, dictionary
initWith.

objectForKey: - ;
valueForKey:
allKeys - ;
allKeysForObject: -
;
allValues - ;
- (void)getObjects:(id *)objects andKeys:(id *)keys -
C;
- (NSArray *)objectsForKeys:(NSArray *)keys notFoundMarker:
(id)anObject -
;


NSMutableDictionary:
- (void)setObject:(id)anObject forKey:(id)aKey -
;
- (void)setValue:(id)value forKey:(NSString *)key -
, nil, ;
- (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary ;
- (void)setDictionary:(NSDictionary *)otherDictionary -
, ;
- (void)removeObjectForKey:(id)aKey -
;
removeAllObjects - ;
- (void)removeObjectsForKeys:(NSArray *)keyArray -
;



Cocoa NSObject.
.h
#import ".h"
// NSObject
// #import<Foundation/NSObject.h>
@interface :
{
// , C
float width;
float height;
BOOL filled;
NSColor *fillColor;
}
//
+ alloc; // + ( static ++/
Java)
- (void)display;
- (float)radius;
//
- (void)setRadius:(float)aRadius;
- (void)setWidth:(float)width height:(float)height;
@end


.m.
//
#import "ClassName.h"
@implementation ClassName
//
+ (id)alloc
{
...
}
- (BOOL)isFilled
{
...
}
- (void)setFilled:(BOOL)flag
{
...
}
@end



NSObject:
alloc - ,
;
allocWithZone - ;
init - .
;
release - .
0, ,
;
retain - ;
autorelease - .
, ;

finalize - ,
-
;
retainCount - ;
,
MyClass
*myClass
=
[[MyClass
alloc]
init];

...
[myClass release]; //

//

new ,
[[MyClass alloc] init].
MyClass *myClass = [MyClass new];

init.
-
nil.
.
, init

- (id)init {
self = [super init]; //
//
if (self) {
/* */
}
return self;
}

init...
- (id)initWithTag:(int)tag;
- (id)initWithTag:(int)tag data:(struct info *)data;

- (id)init
{
return [self initWithTag:-1];
}

- (id)initWithTag:(int)tag
{
return [self initWithTag:tag data:NULL];
}
- (id)initWithTag:(int)tag data:(struct info *)data
{
self = [super init...];
if (self) {
...
}
return self;
}


dealloc
-(void)dealloc {
array = NULL;
[arrayData release];
[super dealloc];
}

, finalize.
- (void)finalize {
if (log_file != NULL) {
fclose(log_file);
log_file = NULL;
}
[super finalize];
}

. malloc/
dealloc, NSAllocateCollectable/finalize.
,
, .


objective-c ,
. objective-c
C++/Java, .
:
-(void) setNumerator: (int) n andDenominator: (int) d;


setNumerator:andDenominator:, setNumerator
.

, .
.
// , frac
[frac setNumerator: 1 andDenominator: 5];

+ -
objective-c .
+, . .
, .

:
@private - ;
@protected - ;
@public - ;
@package - @public ,
@private ;
@interface Worker : NSObject
{
char *name;
@private
char *evaluation;
@protected
id job;
float wage;

@public
id boss;
int age;
}

Worker * worker = [[Worker alloc] init];


worker->age = 30; //
// worker->wage = 60.23; //

2.0 .
/ -
. -
.


@property,
:

readonly - ;

readwrite - ;

assign - ;

retain - ( );

copy - ;

nonatomic - -,
, ;
@interface MyObj : NSObject {
@private int iProp; //
}
//
@property (readwrite,assign) int iProp;
@end


,
iProp. set..., setIProp;
@implementation MyObj
@synthesize iProp;
@end

.
@implementation MyObj
@synthesize anyprop = iProp;
@end


@dynamic.
@implementation MyObj
@dynamic iProp;
-(int)iProp {
NSLog(@" iProp");
return 1;
}
-(void)setIProp:(int)inIProp {
NSLog(@" iProp");
}
@end


, ,
. retain,
, .
- .
(, ,
).
//
iProp = anyval;
//
self.iProp = anyval;
//
MyObj * myObj = [[MyObj alloc] init];
myObj.iProp=anyval;



retain.
- (void) dealloc { [iProp release];

[super dealloc]; }


++. ,
.


@protocol

MyProtocolName

//
- (void) outData;
@end



.
@interface MyClass: NSObject <MyProtocolName>
...
@end

.
@implementation MyClass
- (void) outData {
...
}
...
@end



//
id<MyProtocolName> myProtocolName;
//
- (void)
doSomethingWithThisObject:
aMyProtocolName;

(id<MyProtocolName>)


objective-c .
- . .
.
:
, ,
, Cocoa ;
;
;

,
.
+.h ( +).
//
#import "ClassName.h"1
@interface ClassName (CategoryName)
//
@end


#import "ClassName+CategoryName.h"
@implementation ClassName (CategoryName)
//
@end

-
JavaScript. Ruby.
.


,
objective-c, . SEL
.
.
NSString *str = @"Hello world";
// lowercaseString
SEL sel = @selector(lowercaseString);
// str lowercaseString
NSString *islower = (([str respondsToSelector:sel]) ? @"YES" :
@"NO");
//
NSLog (@" lowercaseString: %@", islower);
// ,
if ([str respondsToSelector:sel]) //(islower == @"YES")
NSLog(@" lowercaseString: %@" , [str
lowercaseString]);

:
@try - , ;
@catch - ;
@finally - ,
;
@throw - ;
@try {
if(/**/) {
@throw exception; //
}
}
@catch (NSException * exception){
// -
[self onException: exception];
}
@finally{
//...
}

NSException
, :
+ exceptionWithName:reason:userInfo: -
,
;
initWithName:reason:userInfo: - ;
raise - ,
;
name - ;
reason - ;
callStackSymbols - ;
// . .
if (pDocument != nil) {
[self loadMetadata];
} else {
[[NSException exceptionWithName:NSInvalidArgumentException
reason:[NSString stringWithFormat:@"Could not open URL %@",
url]
userInfo:nil] raise];
}

@throw raise. ,
:
// ;
NSException* myException = [NSException
exceptionWithName:@"FileNotFoundException"
reason:@"File Not Found on System"
userInfo:nil];
// -
@throw myException;
//
[myException raise];


. 0,
.
, init copy
1. ,
, .
//
NSString * str = [[NSString alloc] initWithFormat: @"%d" , 150];
...
[str release]; //

, init,
new, copy .
- (MyObj *) newMyObj {
return [[MyObj alloc] init];
}

.
1. ,
. release .
,
, .
//
NSString * str = [NSString stringWithFormat:@"%d", 150];
...
//
}

,
, autorelease.

//
return [obj autorelease];


,
.
.
. ,
. ,
.

NSAutoreleasePool ,
autorelease. ,
.
Cocoa .
autorelease, , .
AppKit UIKit
, .
, :
;
;
;
.
// .
NSArray *urls = <# - #>;
for (NSURL *url in urls) {
NSAutoreleasePool *loopPool = [[NSAutoreleasePool alloc] init];
NSError *error = nil;
NSString
*fileContents
=
[[[NSString
alloc]
initWithContentsOfURL:url
encoding:NSUTF8StringEncoding error:&error]
autorelease];
/* - */
...
[loopPool drain];
}

Оценить