-6

This question related to knowing something we don't know. I'm researching with "Why people don't use this"? Is any reason behind this related to specific tech? So read it carefully and give downvote or give correct answer.

We can write

NSMutableString *string = NSMutableString.string;

instead of

NSMutableString *string = [NSMutableString string];

Same as how can we write this method,

NSMutableString *string = [NSMutableString stringWithString:@"test"];

Update:

This question is not an duplicate which is little bit different. And I accept with below answers which is not recommended for good programmers. But They didn't explain Why, for What reason, programmers should avoid this? Could anyone give clear explanation about this with proper link or document?

Mani
  • 17,549
  • 13
  • 79
  • 100
  • 1
    `NSMutableString.string;` should not be used. the `.` syntax should only be used for properties not normal methods. Due to the way Objective-C messages you class it also work for methods without any parameters. But this is not suppose to be used in such a way. – rckoenes Jan 24 '14 at 08:56
  • @rckoenes My friend, where did you see "we should not used this dot for class method"? Please show some reference. – Mani Jan 24 '14 at 08:58
  • 2
    It's a method not a property, the `.` syntax is for properties. From the Apple docs: [Objective-C offers an alternative dot syntax to access an object’s properties.](https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html#//apple_ref/doc/uid/TP40011210-CH5-SW10) – rckoenes Jan 24 '14 at 09:00
  • @rckoenes I know, property should access by `.` ,`getter` or `setter` methods. My question is about `class method`? where did you see `class method should not use `.` operator? – Mani Jan 24 '14 at 09:03
  • @Mani nowhere, but also nowhere is it specified that you should. Your code will become unreadable when mixing property notation and method notation. – rckoenes Jan 24 '14 at 09:12

2 Answers2

2

It's just a syntactic sugar. string method has no arguments so it's treated like a getter, which is not in fact. stringWithString: is method with parameter, so you can't call like that.

In general, I'd not recommend using dot syntax with methods, it's confusing. Objective-C dot notation with class methods?

Update

I don't think there is any technical reason you should avoid it. It's rather in means of coding style, keeping code clean and consistent.

Community
  • 1
  • 1
Maciej Oczko
  • 1,225
  • 8
  • 11
  • I accept with one thing, this is not recommended for all programmers. My question is "why"? for "What"?. Did you means "class method which is not with argument, treated as getter method"? Is this correct? – Mani Jan 24 '14 at 09:23
  • My friend, I know this is not proper coding style. I'm researching with "Why people don't use this"? Is any reason related to specific tech? I need to know that. – Mani Jan 24 '14 at 10:10
2

NSMutableString.string is a hack. It "works" for the same reason that myString.length and [myString length] produce the same result. However, since dot notation is not used with an actual property, it is an abuse of the language feature, because properties have a different semantic. For example, when you access a property multiple times, you naturally expect to get the same result, unless the state of the object has changed in between the two invocations. Since NSMutableString.string produces a new string object on each invocation, it breaks the semantic expected of the "proper" properties, bringing down the readability of your program.

Objective-C does not have a general way of calling a method with arguments using the dot notation. There feature is very specific to properties. Although theoretically you could use MyClass.xyz = abc in place of [MyClass setXyz:abc], but that would be a hack as well.

To answer your question, Objective-C does not offer a way to call [NSMutableString stringWithString:@"test"] with dot notation.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • I can partially accept with you. This statement `dot notation is not used with an actual property` should not be correct, I think? How do you say this? Could you show some link or specs? – Mani Jan 24 '14 at 09:22
  • @Mani I expanded the answer with a short example of what's wrong with calling `NSMutableString.string` through a property syntax. – Sergey Kalinichenko Jan 24 '14 at 09:56
  • I didn't get you fully. Fully confusion in this line `it breaks the semantic expected of the "proper" properties`? – Mani Jan 24 '14 at 10:13
  • @Mani Properties encapsulate the state of an object ([link](http://en.wikipedia.org/wiki/Property_(programming))), so reading a property is expected to give you something related to that object's state. `NSMutableString.string` is not accessing anything related to the state of any object. – Sergey Kalinichenko Jan 24 '14 at 10:32
  • You are absolutely right. But this is not property which shouldn't access any object. It's an class method which is give same result as `[NSMutableString string]`(This is also not an property). So my question is "How it is affect semantic? You can see your link, which doesn't clearly explain about objective-C property?(and also class method of objective-C? I want to say onething, Property is different from class method in objective-C? – Mani Jan 24 '14 at 10:42
  • 1
    @Mani There are two different semantics to view here - one for the compiler, and one for the readers of your code. Compiler sees `[NSMutableString string]` and `NSMutableString.string` as different ways of saying the same thing; it will generate the same exact code. Readers of your program, however, expect that the second expression represented something related to the state. It would take them a few seconds to "decipher" the meaning of your program, which is very bad. – Sergey Kalinichenko Jan 24 '14 at 13:47
  • 1
    @Mani The very fact that you can call a class method is an unfortunate technical coincidence: for some reason, compiler designers did not add code to prohibit that sort of call, so when `x.y` code is translated to `[x y]`, it works for class methods as well. – Sergey Kalinichenko Jan 24 '14 at 13:49