What does the @ symbol represent in objective-c?
I’m learning objective-c and keep bumping into the @ symbol. It is used in different scenarios, for example at the start of a string or to synthesise accessor methods.
What’s does the @ symbol mean in objective-c?
4 Solutions Collect From Internet About “What does the @ symbol represent in objective-c?”
@ character isn’t used in C or C++ identifiers, so it’s used to introduce Objective-C language keywords in a way that won’t conflict with the other languages’ keywords. This enables the “Objective” part of the language to freely intermix with the C or C++ part.
Thus with very few exceptions, any time you see
@ in some Objective-C code, you’re looking at Objective-C constructs rather than C or C++ constructs.
The major exceptions are
Nil, which are generally treated as language keywords even though they may also have a
#define behind them. For example, the compiler actually does treat
id specially in terms of the pointer type conversion rules it applies to declarations, as well as to the decision of whether to generate GC write barriers.
Other exceptions are
bycopy; these are used as storage class annotations on method parameter and return types to make Distributed Objects more efficient. (They become part of the method signature available from the runtime, which DO can look at to determine how to best serialize a transaction.) There are also the attributes within
setter; those are only valid within the attribute section of a
From Objective-C Tutorial: The @ Symbol, the reason it is on the front of various keywords:
Using @ should make it easier to bolt an Objective-C compiler on to an existing C compiler. Because the @ isn’t valid in any context in C except a string literal, the tokenizer (an early and simple step in the compiler) could be modified to simply look for the @ character outside of a string constant (the tokenizer understands string literals, so it is in a position to distinguish this). When @ is encountered the tokenizer would put the rest of the compiler in “Objective-C mode.” (The Objective-C parser would be responsible for returning the compiler back to regular C mode when it detects the end of the Objective-C code).
Also when seen in front of a string literal, it makes an NSString rather than a ‘char *’ in C.
From Macrumors: Objective-C Tutorial, when in front of string literal:
There are also @”” NSString literals. It is essentially shorthand for NSString’s +stringWithUTF8String method.
The @ also adds unicode support to C strings.
From the manual:
Objective-C frameworks typically do not use C-style strings. Instead,
they pass strings around as NSString objects.
The NSString class provides an object wrapper for strings that has all
of the advantages you would expect, including built-in memory
management for storing arbitrary-length strings, support for Unicode,
printf-style formatting utilities, and more. Because such strings are
used commonly though, Objective-C provides a shorthand notation for
creating NSString objects from constant values. To use this shorthand,
all you have to do is precede a normal, double-quoted string with the
@ symbol, as shown in the following examples:
NSString *myString = @"My String\n"; NSString *anotherString = [NSString stringWithFormat:@"%d %@", 1, @"String"];
- UIWebView not freeing all live bytes using ARC
- Xcode, Duplicate symbol _main
- Implementing HMAC and SHA1 encryption in swift
- Custom nav bar styling – iOS
- How do you limit only 1 decimal entry in a UITextField in Swift for iOS8?
- UIPickerView Font
- Passing Data(String) from Child VC to Parent VC using easiest Way
- Converting Swift 2.3 to Swift 3.0 – Error, Cannot invoke 'dataTask' with an argument list of type'
- Simplest way to throw an error/exception with a custom message in Swift 2?
- Finding the Current Active Window in Mac OS X using Python
- How to put a UITextField inside of a UITableViewCell (grouped)?
- Inconsistent Today Widget behavior breaks subview's height constraints
- Issue with getting cell's string using a button in a custom UITableViewCell
- What should my Objective-C singleton look like?
- how to import <sys/utsname.h> in swift