PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, December 12, 2022

[FIXED] What is the difference between "foo.bar" and "[foo bar]" in objective C?

 December 12, 2022     objective-c, syntax     No comments   

Issue

I'm following Apple's Objective C MetalKit guide, and this line of code appears here:

MTLRenderPassDescriptor *renderPassDescriptor = view.currentRenderPassDescriptor;

Is there a difference between using view.currentRenderPassDescriptor; and [view currentRenderPassDescriptor];? I've always seen the second option and neither are giving me errors or different results, but it seems strange that they would have both syntax options available for the same purpose.


Solution

The dot syntax is syntactic sugar, merely a newer, more concise, syntax that they subsequently added to the language. The dot syntax and the square brackets are functionally equivalent.

See Dot Syntax Is a Concise Alternative to Accessor Method Calls from Programming with Objective-C:

As well as making explicit accessor method calls, Objective-C offers an alternative dot syntax to access an object’s properties.

Dot syntax allows you to access properties like this:

NSString *firstName = somePerson.firstName;
somePerson.firstName = @"Johnny";

Dot syntax is purely a convenient wrapper around accessor method calls. When you use dot syntax, the property is still accessed or changed using the getter and setter methods mentioned above:

  • Getting a value using somePerson.firstName is the same as using [somePerson firstName]
  • Setting a value using somePerson.firstName = @"Johnny" is the same as using [somePerson setFirstName:@"Johnny"]

This means that property access via dot syntax is also controlled by the property attributes. If a property is marked readonly, you’ll get a compiler error if you try to set it using dot syntax.



Answered By - Rob
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing