The id data type is, in a sense, a generic data type. It can stand in for Objective-C basic data types (e.g., various types of numbers) or Cocoa Touch objects (e.g., buttons, arrays, and views). In fact, a variable defined as an id data type can even change the type of data it holds during the execution of a method. This flexibility is a hallmark of Objective-C, called dynamic typing (also known as late binding). The idea behind dynamic typing is that a variable doesn’t need to be prewired for only one data type;instead the data type (a class) is assigned at runtime, and the system keeps track of the type as needed (and thus knows to which class it should send messages).
As an illustration of dynamic typing and the id data type, consider an app that defines three very different classes named Leg, Glass, and Meeting. Each class has a method named break, which performs a class-specific breaking action on an instance of that class. As the app runs, a variable of type id could contain an instance of any one of those classes, depending on user interaction or another situation. But when a statement sends the break message to the variable, the actual class of the variable at that instant determines which method executes. Importantly, the compiler can’t know whether a class assigned to the variable has a break method defined for it, because at compile time the class of the variable is indeterminate.
Before you draw the conclusion that you can simply adopt the id data type for everything and treat all Objective-C variables as loosely typed JavaScript variables, hold your horses! The compiler will let potential data type incompatibilities slide by if you are sloppy with typing. Errors will occur at runtime and conceivably generate crashes that can be challenging to repair even with the Xcode debugger. You are often better off finding data type inconsistencies at compile time rather than at runtime. The Xcode compiler supplies many clues (albeit not all of them immediately clear to newcomers) about data type issues. It’s true that accurately typing your variables and methods may be frustrating to work with at first—you’ll feel as though you’re doing a lot of the work the compiler should be doing—but the code will be easier to maintain over time.
Despite these cautions, don’t be afraid of dynamic typing—Cocoa Touch frameworks use it extensively. It will just take some time working in Objective-C before you recognize places where dynamic typing can contribute to your code’s flexibility.
分享到:
相关推荐
google的算法正在变化,看看其中的秘密。 Google’s-Secret-Sauce-Revealed-The-Death-and-Life-of-Search-Engine-Optimization
"Starting in the early 1960s, there was fear in America about the proliferation of computer database and networking technologies. People worried that these systems were going to be used by both ...
book of secret knowledge master
jsonwebtoken-secret-gen-源码.rar
Hacking-Secret-Ciphers-with-Python-A-beginner-s-guide-to-cryptography-and-computer-programming-with-Python.pdf
python库。 资源全名:secret_loader-0.5-py3-none-any.whl
python库。 资源全名:cloudcomponents.cdk-secret-key-1.14.0.tar.gz
资源来自pypi官网。 资源全名:cloudcomponents.cdk-secret-key-1.12.1.tar.gz
python库,解压后可用。 资源全名:secret_miner-0.2.5-py2.py3-none-any.whl
资源来自pypi官网。 资源全名:secret_miner-0.2.5-py2.py3-none-any.whl
git-secret什么是git-secret ? git-secret是一个bash工具,可将私人数据存储在git repo中。 git-secret使用允许的用户公钥加密文件,从而使您信任的用户可以使用pgp及其私钥访问加密的数据。 使用git-secret ,可以...
资源分类:Python库 所属语言:Python 资源全名:ga_secret_generator-0.1.1-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源分类:Python库 所属语言:Python 资源全名:aws_secret_cdk-2.0.2-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源来自pypi官网。 资源全名:cquest_secret_manager-0.3.0.tar.gz
资源来自pypi官网。 资源全名:aws_secret_cdk-1.0.7.tar.gz
TSTripleSec Objective-C实现。 TripleSec是三偏执的对称加密,因此一两个密码失败不会破坏数据。 有关此处使用的加密实现的更多详细信息,请参见 。播客文件 pod "TSTripleSec"TSTripleSec # import < TSTripleSec>...
现在,库执行以下操作: 下载并解析行程数据下载并解析行程的车辆和位置(“终点”)数据将行程路径解析为MKPolyline对象(包括单独的子规范) 甜蜜的GIF动作:用法只需将#import 在你正在做的任何 Objective-C ...