I have experience on C and Python, I learned the Object-C today,
I want to make sure if my concept is correct ?
I don't know why should I put a statement in a bracket
[pt setX: 8];
Isn't pt setX: 8
meaningful enough ?
If brackets is only for readable, why I got errors in this picture, I just want to know when should I use the bracket , and when isn't need.
Is pt setX: 8
similar to pt.setX(8)
in Python or C-like language?
To create a object,
You have to define .h
.m
,
In C
, you can define and implement
both in a .c
file , but can not in object-c ?
If you want autorelease the object memory without explicitly free the memory in manual,
Just put your code in the @autoreleasepool
block, right ?
@autoreleasepool {
MyPoint* pt = [MyPoint alloc];
// call constructor
pt = [pt init];
[pt print];
[pt getArea];
[pt setX: 8];
[pt setY: 99];
[pt print];
[pt getArea];
}
MyPoint.m
// // MyPoint.m // hello_world // // Created by poc on 2014/4/27. // Copyright (c) 2014年 poc. All rights reserved. //
import "MyPoint.h"
@implementation MyPoint
- (void) print
{
NSLog(@"X =%i and Y= %i", _x, _y);
}
- (void) getArea
{
NSLog(@"Area is %i", _x*_y);
}
- (void) setX:(int)aX
{
_x = aX;
}
- (int) getX
{
return _x;
}
- (void) setY:(int)aY
{
_y = aY;
}
- (int) getY
{
return _y;
}
@end