-1

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.

enter image description here

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
Community
  • 1
  • 1
newBike
  • 14,385
  • 29
  • 109
  • 192

2 Answers2

0

Is pt setX: 8 not meaningful enough ?

No, it's not. Let's assume situation when you want use result of method invocation as object

pt area intValue // without brackets it's a mess
[[pt area] intValue]; // looks readable now

Is pt setX: 8 similar to pt.setX(8) in Python or C-like language?

Yes, it similar. But you need square brackets

In C, you can define and implement both in a .c file , but can not in object-c ?

Analog of .c file for obj-c is named .m. There you can do the same stuff as you can in .c, importing of .m is really bad practice, it leads you in majority of cases to incorrect linking and, eventually in failure of build. Also it good way to separate private and public interfaces. .h contains public interface to class, you import .h and see only public methods and class variables, while .m contains private methods and variable that you don't want to expose.

If you want autorelease the object memory without explicitly free the memory in manual,

Just put your code in the @autoreleasepool block, right ?

No, it's not. You can't count on autoreleasing of variables you puts there. Autorelease pool created for another purpose. If you want to know why - read this. I encourage you to use ARC (automatic reference counting), it's enabled by default in latest project templates in Xcode. This way you don't need to worry about memory management, while you correctly use naming convention.

Ossir
  • 3,109
  • 1
  • 34
  • 52
  • do you think the brackets is only for readable without other meaning :D – newBike Apr 27 '14 at 08:05
  • I updated my post, 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. – newBike Apr 27 '14 at 08:13
  • You always need to use brackets, like you always need to use `.` dot when executing method in python. If you have no chance to read it write, how compiler can parse it? – Ossir Apr 27 '14 at 11:05
  • You use brackets when you want to send message (call method) on some object. All `c` also valid in `obj-c` and you don't need to use square brackets with `c` construction like `=` (equality). Without square brackets your code just set of variable which is not what you want here `pt getArea;`. – Ossir Apr 27 '14 at 11:10
0

I don't know why should I put a statement in a bracket

That's just the syntax for method calls. Why are parentheses required around arguments in C and Python? Syntax. Other languages don't require parentheses or brackets for method and function calls (e.g. Smalltalk, Ruby and Perl, though parentheses allow you to be more expressive) because they use different syntax. Smalltalk, in particular, is similar to Objective-C method calls, but without the brackets (not coincidentally, since Smalltalk inspired Objective-C).

Is pt setX: 8 similar to pt.setX(8) in Python or C-like language?

Yes. In particular, [pt setX: 8] calls method setX on (or, if you prefer, sends message setX to) object pt, passing 8 as a parameter.

In C, you can define and implement both in a .c file , but can not in object-c ?

Keep in mind that anything declared solely in a compilation unit (a ".c" file) isn't accessible in other compilation units. With Objective-C, you can declare both static variables (as you might in C) and methods in a compilation unit (which have that ".m" extension), but they are effectively private.

If you want autorelease the object memory without explicitly free the memory in manual, [j]ust put your code in the @autoreleasepool block, right ?

An @autoreleasepool block isn't responsible for keeping track of object lifetime; see "Why is autorelease (@autoreleasepool) still needed with ARC?" for what it does. See also "Transitioning to ARC Release Notes" for info on Automatic Reference Counting (ARC) and "Memory management" for info on the older approach (manual reference counting), which is what ARC does under the hood.

Community
  • 1
  • 1
outis
  • 75,655
  • 22
  • 151
  • 221