C
( C Objective-C ). :
-
Smalltalk ( ),
, (BOOL
, YES
NO
), .
C++ . Objective-C runtime . , . (, ). . Objective C . . , .
objc_sendmsg(object, selector, arguments). , , . , :
[mycalendar ordinalityOfUnit:myunit inUnit:myunit2 forDate:mydate]
… “ordinalityOfUnit:inUnit:forDate:”. objc_sendmsg , 32- . , .
arguments varargs C.
fragile base class. , API . , Cocoa- NSController, . , API , . Apple API. , Camino Roxio Toast Titanium, . , , . , , id, .
Java, Objective C . , , .
id. id void , id, void. id. . . , NSController*, . , . , NSController NSController, . , ( runtime ), . Objective C ( objc_), , , .
Objective-C ( Ruby). , (), .
:
@””. NSString, char*, C.
#include Objective-C #import, .
(framework). , .framework. ( ) Headers, Resources, Frameworks. #import <Cocoa/Cocoa.h> Cocoa.framework/Headers/Cocoa.h .
// | |
, | #if 0 ... #endif |
, | /* ... */ |
Yes | |
[_a-zA-Z][_a-zA-Z0-9]* | |
variable = value | |
type variable | |
type variable = value | |
{ ... } | |
== | |
!= | |
< > <= >= | |
returntype f(type1 p1, type2 p2, ...) | |
f(a, b, ...) | |
f() | |
; | |
- | if (condition) ... |
- - | if (condition) ... else ... OR ( condition ? ... : ... ) |
while (1) ... | |
while (condition) ... | |
do ...while (!condition) | |
for - next 1 | for (int i = 1; i <= 10; i++) ... |
for - next 1 | for (int i = 10; i >= 1; i--) ... |
Objective-C 80-x Smalltalk. (, ), ( ). , Objective-C C. - : , Objective-C ( ), , Objective-C. , ++. 1988 NeXT ( Apple) Objective-C ( SDK). 1992 GNU OpenStep. GCC Objective-C. NeXT, Apple SDK (, , IDE) . IDE Xcode, GUI Interface Builder. Cocoa GUI ( ) Objective-C.
Objective-C .m ( ++ Objective-, .mm). .h. , Objective- . id, ( void *). nil. , id. : , id? isa, , NSObject ( NS NeXT Step). isa Class. , , , ( ). Objective-C, , @ ( @protocol, @selector, @interface). (@private, @protected) . Cocoa NSString. @Hello world, Hello world. BOOL ( unsigned char) YES NO. Objective-C ( objc/objc.h) :
- , , . . :
[receiver method];
[receiver method: 20.0 : 30.0];
[receiver methodWithFirstArgument: 10 andSecondArgument: 20];
[receiver undefinedNumberParameters: one, two, three, four, five, six, seven];
BOOL booleanValue;
booleanValue = [reveiver method];
if ([anObject respondsToSelector: @selector(myMethodWith2Argumets::)])
{
//
[anObject myMethodWith2Argumetns: @first : @second];
}
else
{
//
}
- :
id objc_msgSend(id receiver, SEL method, ...);
[receiver ddObject: otherObject];
objc_msgSend(receiver, 12, otherObject);
addObject(receiver, otherObject);
, addObject :
- (void)addObject: (id)otherObject;
-|+ (< >)
[ : (< >)
[ [] : (< >)]
]
+ (Class)class;
+ (id)alloc;
- (id)init;
- (void)addObject: (id)anObject;
+ (NSString *)stringWithCString: (const char*)aCString usingUncoding: (enum NSStringEncoding)encoding;
- (NSString *)initStringWithFormat: (NSString *)format, ;
[myLabel setText: [[NSString stringWithString: @Hello] stringByAppendingString: @ world]];
Complex.h:
#import <Foundation/Foundation.h> // NSObject NSString
@interface Complex : NSObject
{
double _re; //
double _im; //
NSString *_format; // description
}
- (id)initWithRe: (double)re andIm: (double)im; //
+ (Complex *)complexWithRe: (double)re andIm: (double)im; //
- (Complex *)add: (Complex *)other; //
- (Complex *)sub: (Complex *)other; //
- (NSString *)format; // _format
- (void)setFormat: (NSString *)format; // _format
- (double)re; //
- (void)setRe: (double)re;
- (double)im;
- (void)setIm: (double)im;
@end
, , c : 1) ( alloc) 2) ( ).
MyClass *myObject = [[MyClass alloc] init]; // MyClass alloc , init myObject
NSMutableArray *array = [[NSMutableArray alloc] init]; //
MyClass *myObject = [[MyClass alloc] init]; //
[myObject myMethod]; //
[array addObject: myObject]; //
MyClass *otherObject = [array getLastObject:]; // ,
[otherObject myOtherMethod: YES]; // BOOL
+ (NSString *)stringWithCString: (char const *)string encoding: (NSStringEncoding)encoding;
NSString *myString = [NSString stringWithCString: Bla-bla-bla encoding: NSASCIIStringEncoding];
, , , (, , ) . Objective-C . , , . , , , . alloc, . retain, release. NSObject, . , NSString ( @I am a string) -1, . :
id anObject = [SomeClass alloc]; // == 1
[anObject init]; //
[anObject reatin]; // ( == 2)
[anObject release]; // ( == 1 )
[anObject release]; // , 1
- (id)init
{
self = [super init]; //
//
if (self) //
// , nil
{
//
}
return self; //
}
- (id)initWithInt: (int)number
{
if (self = [super init])
{
_myMember1 = [[SomeClass alloc] init]; // : ,
_myMember2 = [[SomeClass alloc] init];
_myIntMember = number; //
//
}
return self;
}
- (void)retain
{
[_internalLock lock]; //
_referenceCounter++; // _referenceCounter
[_internalLock unlock];
}
- (void)release
{
[_internalLock lock];
_referenceCounter--; //
if (!_referenceCounter) //
{
[_internalLock unlock];
[self dealloc]; // , ( )
}
[_internalLock unlock];
}
- (void)dealloc
{
[_myMember1 release]; //
[_myMember2 release]; //
//[_myIntMember release]; , ..
[super dealloc]; //c ,
}
. , . , , , , , set:
- (void)setRe: (double)re
{
_re = re;
}
//,
[label setText: @Hello world]; // text
// label NSString *
// setText UILabel ( 1)
- (void)setText: (NSString *)text
{
[text retain]; //
[_text release]; // _text
_text = text; //
}
// setText UILabel ( 2)
- (void)setText: (NSString *)text
{
if (_text != text) //c
{
[_text release]; //
// _text
_text = [text retain]; //
//
}
}
// setText UILabel ( 3 )
- (void)setText: (NSString *)text
{
if (_text != text)
{
[_text autorelease]; // e
// _text
_text = [text retain]; //
//
}
}
- (NSString *)text
{
return _text;
}
:
-(NSString *)sayHelloToName: (NSString *)name withSurname: (NSString *)surname
{
NSString *retString = [[NSString alloc] initWithFormat: @%@ %@!, name, surname]; //
return retString;
}
- (NSString *)description
{
return [NSString stringWithFormat: @re: %lf im: %lf, _re, _im]; // @re: 1.0 im: 2.5 _re == 1.0 _im == 2.5
}
int main(int argc, char *argv[]) // main()
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // ,
int retVal;
//
[pool drain]; // , autorelease
return retVal;
}
-(NSString *)sayHelloToName: (NSString *)name withSurname: (NSString *)surname
{
NSString *retString = [[NSString alloc] initWithFormat: @%@ %@!, name, surname]; //
[retString autorelease]; // , retString
return retString;
}
, drain release , , , . , Mac OS 10.4 , iOS .
Complex.m Complex:
#import Complex.h
@implementation Complex
- (id)init
{
return [self initWithRe: 0.0 andIm: 0.0];
}
- (id)initWithRe: (double)re andIm: (double)im
{
if (self = [super init])
{
_re = re;
_im = im;
_format = @re: %.1lf im: %.1lf; //
}
}
+ (Complex *)complexWithRe: (double)re andIm: (double)im
{
return [[[Complex alloc] initWithRe: re andIm: im] autorelease];
}
- (Complex *)add: (Complex *)other
{
return [[Complex alloc] initWithRe: _re + other->_re andIm: _im + other->_im];
}
- (Complex *)sub: (Complex *)other
{
return [[Complex alloc] initWithRe: _re other->_re andIm: _im other->_im];
}
- (NSString *)format
{
return _format;
}
- (void)setFormat: (NSString *)format
{// -
[format retain];
[_format release];
_format = format;
}
- (double)re
{
return _re;
}
- (void)setRe: (double)re
{
_re = re;
}
- (double)im
{
return _im;
}
- (void)setIm: (double)im
{
_im = im;
}
- (NSString *)description
{//
return [NSString stringWithFormat: _format, _re, _im];
}
- (void)dealloc
{
[_format release]; // dealloc
[super dealloc];
}
@end
#import Complex.h
#import <stdio.h> // printf()
int main()
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Complex *num1 = [[Complex alloc] init]; //0.0+0.0*i
Complex *num2 = [[Complex alloc] initWithRe: 1.5 andIm: -2];
//1.5-2.0*i
Complex *num3 = [Complex complexWithRe: 5 andIm: 7];
//5.0+7.0*i
printf(%s\n, [[num2 description] cStringUsingEncoding: NSASCIIStringEncoding]); //> re: 1.5 im: -2.0
printf(%s\n, [[[num2 add: num3] description] cStringUsingEncoding: NSASCIIStringEncoding]); //> re: 6.5 im: 5.0
[num1 setRe: [num2 re]]; // _re num1 num2
[num1 setIm: [num3 im]]; // _im num1 num3
[num1 setFormat: @%.2lf+%.2lf*i]; // num1
printf(%s\n, [[num1 description] cStringUsingEncoding: NSASCIIStringEncoding]); //> 1.50+7.00*i
[num1 release];
[num2 release];
//[num3 release]; , ..
[pool drain];
return 0;
}
(, , ) \ :
// CategorizedComplex.h
#import Complex.h
@interfce Complex (CategorizedComplex)
- (Complex *)mul: (Complex *)other;
- (Complex *)div: (Complex *)other;
@end
// CategorizedComplex.m
#import CategorizedComplex.h
@implementation Complex (CategorizedComplex)
- (Complex *)mul: (Complex *)other
{
return [Complex complexWithRe: _re * other->_re - _im * other->_im andIm: _re * other->_im + _im * other->_re];
}
- (Complex *)div: (Complex *)other
{
double retRe, retIm, denominator;
denominator = other->_re * other->_re + other->_im * other->_im;
if (!denominator)
return nil;
retRe = (_re * other->_re + _im * other->_im) / denominator;
retIm = (_im * other->_re - _re * other->_im) / denominator;
return [Complex complexWithRe: retRe andIm: retIm];
}
@end
CategorizdComplex *num1 = [[CategorizedComplex alloc] initWithRe: 1 andIm: 999];
Complex *num2 = [Complex complexWithRe: 0 andIm: 0];
CategorizedComplex *num3 = [num1 div: num2]; //num3 == nil
:
// CategorizedComplex.m
#import CategorizedComplex.h
@interface Complex ()
- (void)zeroComplex; //
@end
@implementation Complex
- (void)zeroComplex //
{
_re = 0;
_im = 0;
}
@end
Objective-C , , , ( ++, virtual = 0). 2.0 ( @required, ) ( @optional). , , . , , , :
@protocol MyPrinterProtocol
@required
- (void)print;
- (BOOL)switchedOn;
@optional
- (void)loadPapaer: (int)numberOfPages;
@end
@interface MyPrinter : NSObject <MyPrinterProtocol>
// MyPrinter MyPrinterProtocol
{
BOOL _state;
int _numberOfPages;
}
- (id)initWithState: (BOOL)state andPagesCount: (int)pages;
- (BOOL)state;
@end
MyPrinter *printer;
id anotherPrinter = [[MyPrinter alloc] init];
[anotherPrinter print]; //
, . .
@interface MyPrinter : NSObject <MyPrinterProtocol, OtherProtocol>
id <MyPrinterProtocol> somePrinter;
: , , . , , , , , , . Objective-C .
. ( ) . Cocoa NSException, NSString ( id):
- (id)initWitnName: (NSString *)name reason: (NSString *)reason userInfo: (id)userInfo;
Cup *cup = [[Cup alloc] init];
@try
{
[cup fill]; // fill NSException
}
@catch (NSException *exception)
{// NSLog
NSLog(@"main: Caught %@: %@", [exception name], [exception reason]);
}
@finally // @try
{
[cup release];
}
@finally , @try, .
Objective-C 2.0 a Complex : . :
// Complex.h
#import <Foundation/Foundation.h> // NSObject NSString
@interface Complex : NSObject
{
double _re; //
double _im; //
NSString *_format; // description
}
- (id)initWithRe: (double)re andIm: (double)im;
+ (Complex *)complexWithRe: (double)re andIm: (double)im;
- (Complex *)add: (Complex *)other; //
- (Complex *)sub: (Complex *)other; //
@property (nonatomic, retain) NSString *format; //
@property (nonatomic, assign) double re; //
@property (nonatomic, assign) double im;
@end
// Complex.m
#import Complex.h
@implementation Complex
@synthesize format = _format; //
@synthesize re = _re; //
@synthesize im = _im; //
- (id)init
{
return [self initWithRe: 0.0 andIm: 0.0];
}
- (id)initWithRe: (double)re andIm: (double)im
{
if (self = [super init])
{
_re = re;
_im = im;
_format = @re: %.1lf im: %.1lf; //
}
}
+ (Complex *)complexWithRe: (double)re andIm: (double)im
{
return [[[Complex alloc] initWithRe: re andIm: im] autorelease];
}
- (Complex *)add: (Complex *)other
{
return [[Complex alloc] initWithRe: _re + other.re andIm: _im + other.im]; // re im
}
- (Complex *)sub: (Complex *)other
{
return [[Complex alloc] initWithRe: _re other.re andIm: _im other.im]; // re im
}
@end
, .. . , .
Complex . , .
10.11.2021 - 12:37: - Personalias -> WHO IS WHO - - _. 10.11.2021 - 12:36: - Conscience -> . ? - _. 10.11.2021 - 12:36: , , - Upbringing, Inlightening, Education -> ... - _. 10.11.2021 - 12:35: - Ecology -> - _. 10.11.2021 - 12:34: , - War, Politics and Science -> - _. 10.11.2021 - 12:34: , - War, Politics and Science -> . - _. 10.11.2021 - 12:34: , , - Upbringing, Inlightening, Education -> , - _. 10.11.2021 - 09:18: - New Technologies -> , 5G- - _. 10.11.2021 - 09:18: - Ecology -> - _. 10.11.2021 - 09:16: - Ecology -> - _. 10.11.2021 - 09:15: , , - Upbringing, Inlightening, Education -> - _. 10.11.2021 - 09:13: , , - Upbringing, Inlightening, Education -> - _. |