Dynamic typing 动态类型
https://developer.apple.com/library/content/documentation/General/Conceptual/DevPedia-CocoaCore/DynamicTyping.html#//apple_ref/doc/uid/TP40008195-CH62-SW2
A variable is dynamically typed when the type of the object it points to is not checked at compile time. Objective-C uses the id data type to represent a variable that is an object without specifying what sort of object it is. This is referred to as dynamic typing.
Dynamic typing contrasts with static typing, in which the system explicitly identifies the class to which an object belongs at compile time. Static type checking at compile time may ensure stricter data integrity, but in exchange for that integrity, dynamic typing gives your program much greater flexibility. And through object introspection (for example, asking a dynamically typed, anonymous object what its class is), you can still verify the type of an object at runtime and thus validate its suitability for a particular operation.
The following example illustrates dynamic typing using a heterogeneous collection of objects:
NSArray *anArray = [NSArray arrayWithObjects:@"A string", [NSDecimalNumber zero], [NSDate date], nil]; |
NSInteger index; |
for (index = 0; index < 3; index++) {
|
id anObject = [anArray objectAtIndex:index]; |
NSLog(@"Object at index %d is %@", index, [anObject description]); |
} |
The object pointed to by the variable at runtime must be able to respond to whatever messages you send to it; otherwise, your program throws an exception. The actual implementation of the method invoked is determined using dynamic binding.
The isa Pointer
Every object has an isa instance variable that identifies the object's class. The runtime uses this pointer to determine the actual class of the object when it needs to.
Dynamic typing 动态类型的更多相关文章
- dynamic:动态类型简单用法,写法
class 动态创建数据 { //动态类型:本质感觉跟object的用法差不多,只是在执行的时候才知道数据类型 public dynamic Dynamic() { //定义一个动态类型,作为返回值 ...
- Net 4.0 之 Dynamic 动态类型
Net 4.0 之 Dynamic 动态类型 本文主要旨在与网友分享.Net4.0的Dynamic 对Duck Type 的支持. 一..net4.0主要新特性 .Net4.0在.Net3.5 ...
- C#基本语法 - .Net 4.0 之 Dynamic 动态类型
一..net4.0主要新特性 .Net4.0在.Net3.5基础上新增的主要特性有:可选参数.命名参数和Dynamic.具体请阅生鱼片的这篇博文.这里我们着重讲解C#4.0的Dynamic特性,对 ...
- c# dynamic动态类型和匿名类
dynamic类型 简单示例 dynamic expando = new System.Dynamic.ExpandoObject(); //动态类型字段 可读可写 expando.Id = 1; e ...
- C# 匿名对象(匿名类型)、var、动态类型 dynamic
本文是要写的下篇<C#反射及优化用法>的前奏,不能算是下一篇文章的基础的基础吧,有兴趣的朋友可以关注一下. 随着C#的发展,该语音内容不断丰富,开发变得更加方便快捷,C# 的锋利尽显无疑. ...
- C# 匿名对象(匿名类型)、var、动态类型 dynamic——实用之:过滤类属性、字段实用dynamic
例子 返回一个LIst<oject>类型 而oject含有 30个字段 而我只需要两个字段.这里实用dynamic 和 linq. 上代码: 注意select new {} 为匿名类型,这 ...
- var和dynamic的应用 var、动态类型 dynamic 深入浅析C#中的var和dynamic ----demo
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- c# 把一个匿名对象赋值给一个Object类型的变量后,怎么取这个变量? c# dynamic动态类型和匿名类 详解C# 匿名对象(匿名类型)、var、动态类型 dynamic 深入浅析C#中的var和dynamic
比如有一个匿名对象,var result =......Select( a=>new { id=a.id, name=a.name});然后Object obj = result ;我怎 ...
- 使用动态类型dynamic让你的省了很多临时类
客户端与服务端的数据交互使用的数据格式是json格式,为了使客户端与服务端有类对应关系,进行序列化,所以总要定义一些类,使用动态类型dynamic可以不必要定义那么多类. 测试代码: using Sy ...
随机推荐
- Jade(Pug) 模板引擎使用文档
本篇内容 在 Express 中调用 jade 模板引擎 jade 变量调用 if 判断 循环 Case 选择 在模板中调用其他语言 可重用的 jade 块 (Mixins) 模板包含 (Includ ...
- Spring基本原理模拟(IoC部分)
package ioc; import java.io.File; import java.lang.reflect.Method; import java.util.Collections; imp ...
- Hibernate单表映射学习笔记之一——hibernalnate开发环境配置
1.什么是ORM? Object/Relationship Mapping:对象/关系映射 2.写SQL语句不好之处: (1)不同数据库使用的SQL语法不同(PL/SQL.T/SQL) (2)同样的功 ...
- Spark 性能调优零散知识
1. 如果 Spark 中 CPU 的使用率不够高,可以考虑为当前的程序分配更多的 Executor, 或者增加更多的 Worker 实例来充分的使用多核的潜能 2. 适当设置 Partition 分 ...
- LibreOJ #6192. 「美团 CodeM 复赛」城市网络
#6192. 「美团 CodeM 复赛」城市网络 内存限制:64 MiB时间限制:500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: sqc 提交提交记录统计讨论测试数据 题目描 ...
- bzoj3876: [Ahoi2014&Jsoi2014]支线剧情(上下界费用流)
传送门 一道题让我又要学可行流又要学zkw费用流…… 考虑一下,原题可以转化为一个有向图,每次走一条路径,把每一条边都至少覆盖一次,求最小代价 因为一条边每走过一次,就要付出一次代价 那不就是费用流了 ...
- EIGRP-1-EIGRP的基础和演变
值得一提的是,在2013年,Cisco决定开放EIGRP的定义,并将其发布为IETFInternet草案,即RFC的前身:文档名称为draft-savage-eigrp.从此,基本的EIGRP不再是机 ...
- Angular学习笔记【如何正确使用第三方组件】
例如:ng-bootstrap的使用: 1.首先肯定是先要安装,参考官网给出的指令安装即可.(npm install --save @ng-bootstrap/ng-bootstrap) 2.在App ...
- POJ2758 Checking the Text 哈希
注意到插入次数挺少的,于是每次暴力重构,然后哈希+二分 #include<cstdio> #include<iostream> #include<algorithm> ...
- php与MySQL与echart综合使用
http://www.yinghualuowu.com/php/echart.html 创建table sex 有name num <?php ini_set('disp ...