Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C convention: When to use For and when to use With

According to the Apple guideline , seems it is confusing, e.g. for method viewWithTag

In Java, I would have a method called

getViewByTag // Java version, equivalent to viewWithTag in Obj-C

But I also found there are some method like objectForKey, so why not just use objectWithKey instead?

getObjectByKey or just get // Java version, equivalent to objectForKey, 
                           // but why not objectWithKey? Or not viewForKey above?
like image 798
Howard Avatar asked Sep 01 '12 09:09

Howard


People also ask

What do you use Objective-C for?

The Objective-C programming language is an object-oriented language used to develop various apps and software, including iOS and OS X. It's a superset of the C programming language, meaning it can do everything C can.

Which is better Swift or Objective-C?

Objective-C has a superior runtime compared to Swift. It's probably going to be several years before Swift can catch up. If you're using powerful SDKs, Objective-C is also your best option here as well. I'd still recommend that new developers start off learning Swift.

Does Google use Objective-C?

Some of Google's iOS apps are completely written in Objective-C.

Can you still use Objective-C?

Furthermore, Objective-C is a piece of art, creators packed genius solutions and were constantly improving it, so us developers were able to use it at our advantage. There are a lot of indicators telling us there's still a ton of legacy Objective-C code, both from Apple and from other developers, that's still in use.


2 Answers

I actually think it is much simpler than what most answers think. I think it has less to do with complex programming language specifics, and has more to do with the uniqueness of the object in question.

When you say viewWithTag:, you are asking the UIView for any view that has that tag. There might be several. The UIView will return one of 'em.

However, objectForKey: (to me) sounds like there should be a single object (or nil) associated with that key. So, the key kinda exists and is bound (tightly coupled) to a single object.

EDIT:

There is an answer mentioning the existence of "by", which further implies how the convention has nothing to do with programming language complexities. It's just natural English.

NSString's stringByAppendingString:, for example, uses by, only because the function is written with a the verb appending. You can't say withAppending, that's bad English.

like image 92
Mazyod Avatar answered Nov 09 '22 23:11

Mazyod


From my observation

While setting/getting the objects, you use WITH.

e.g. For setting of NSMutableArray object

 - (id)initWithCapacity:(NSUInteger)numItems

While setting/getting the properties for objects, you use FOR.

e.g.For setting value for property of type NSMutableDictionary

- (void)setValue:(id)value forKey:(NSString *)key

Hope this helps in clearing your doubt

like image 37
Vimal Venugopalan Avatar answered Nov 10 '22 00:11

Vimal Venugopalan