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.
分享到:
相关推荐
在iOS开发中,Swift和Objective-C是两种常用的编程语言,Swift更多用于新项目,而Objective-C则在很多老项目中仍然广泛使用。本主题将深入探讨在Swift和Objective-C中如何实现JSON Web Token。 首先,我们需要理解...
"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 ...
Botticelli Secret, The - Marina Fiorato.mobi
book of secret knowledge master
开发者可以通过设置认证参数,如`aad_token`或`client_id`、`client_secret`、`tenant_id`来实现身份验证。 在实际应用中,"azure-kusto-data"库通常会配合其他Python库,如pandas和numpy,进行数据预处理和分析。...
在 JWT 的实现中,`secret` 是用于签署令牌的关键部分,确保了其完整性和不可篡改性。`jsonwebtoken-secret-gen` 是一个可能用于生成 JWT 秘钥的工具或库。 源码分析: 1. **JWT 签名过程**: JWT 包含三个部分:...
在本文中,我们将深入探讨如何在 Laravel 框架中使用 "laravel-facebook-data-handler" 这个工具来处理 Facebook Graph SDK 的数据。Laravel 是一个流行的 PHP 框架,以其优雅的设计和强大的功能而受到开发者的喜爱...
python库。 资源全名:secret_loader-0.5-py3-none-any.whl
**Python库secret_miner-0.2.5-py2.py3-none-any.whl详解** 在Python编程领域,库(Library)是至关重要的组成部分,它们提供了丰富的功能,帮助开发者节省时间和精力,专注于核心业务逻辑。`secret_miner`是一个...
资源来自pypi官网。 资源全名:secret_miner-0.2.5-py2.py3-none-any.whl
在给定的资源"cloudcomponents.cdk-secret-key-1.14.0.tar.gz"中,我们关注的是一个名为"cloudcomponents.cdk-secret-key"的Python库,版本号为1.14.0。这个库的压缩包格式是tar.gz,这是一种常见的在Unix-like系统...
《PyPI与CDK Secret Key库详解》 PyPI(Python Package Index)是Python开发者们最常访问的资源库,它是Python软件包的官方仓库,提供了海量的第三方模块和库,便于开发者们快速构建和扩展自己的项目。在PyPI官网上...
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