obj-c 名词
类: Class (description/template for an object)
实例: Instance (manifestation of a class)
消息: Message (sent to object to make it act)
方法: Method (code invoked by a Message)
实例变量: Instance Variable (object-specific storage)
超类/子类: Superclass/Subclass (Inheritance)
协议: Protocol (non-class-specific methods)
Class variable defined at @implementation and @interface?
@interface Someclass : NSObject {
NSString *forExample;
}
@end vs. @implementation Someclass
NSString *anotherExample;
-(void)methodsAndSuch {}
@end
They're very different! The one in @implementation
is a global variable not unique to each instance. Imagine there were accessors for both variables, written in the obvious way. Then the difference in behavior is shown here:
Someclass* firstObject = [[Someclass alloc] init];
Someclass* secondObject = [[Someclass alloc] init]; //forExample is an instance variable, and is unique to each instance.
[firstObject setForExample:@"One"];
[secondObject setForExample:@"Two"];
NSLog(@"%@",[firstObject forExample]); //Result: "One"
NSLog(@"%@",[secondObject forExample]); //Result: "Two" //anotherExample is a global variable, and is NOT unique to each instance.
[firstObject setAnotherExample:@"One"];
[secondObject setAnotherExample:@"Two"];
NSLog(@"%@",[firstObject anotherExample]); //Result: "Two" (!)
NSLog(@"%@",[secondObject anotherExample]); //Result: "Two" //Both instances return "Two" because there is only ONE variable this time.
//When secondObject set it, it replaced the value that firstObject set.
If you are looking for this sort of behavior, you might be better off using a class variable, like this:
static NSString* yetAnotherExample = nil;
Then you can use class methods to interact with the variable, and it's clearly class-specific (as opposed to instance-specific or global).
instance-specific variable declared at @interface
global variable variable declared at @implementation
class variable variable declared using static
static variables in Objective-C
In both C and Objective-C, a static variable is a variable that is allocated for the entire lifetime of a program. This is in contrast to automatic variables, whose lifetime exists during a single function call; and dynamically-allocated variables like objects, which can be released from memory when no longer used. More simply put, a static variable's value is maintained throughout all function/method calls. When declared outside of a function, a static variable is visible to everything within the file in which it is declared; when declared inside a function or method, it is visible only within that function or method, but the value is retained between calls.
Say you have this:
int f(void)
{
int i = 5;
i += 10;
return i;
}
Every call to f()
will return the value 15
.
Now say you have this:
int g(void)
{
static int i = 5;
i += 10;
return i;
}
The first time g()
is called, the value 15
will be returned. The second time, 25
will be returned, as i
maintained its value of 15
and then incremented itself by 10
. The third call, 35
will be returned. And so on.
In the context of Objective-C classes, static variables are often used to mimic class variables, as Objective-C does not have class variables (other languages, such as Java, do). For instance, say you want to lazily initialize an object, and only return that object. You might see this:
static MyObject *obj = nil;
@implementation MyObject
+ (id)sharedObject
{
if (obj == nil) obj = [[MyObject alloc] init];
return obj;
}
@end
obj
will be initialized the first time classObject
is called; subsequent invocations of classObject
will return the same object. You could check this by logging the address of the object:
NSLog(@"obj is at %p", [MyObject sharedObject]);
NSLog(@"obj is at %p", [MyObject sharedObject]); // Will print the same address both times
Furthermore, obj
will be visible to all methods in MyObject
.
This technique is used to implemented singleton classes in Objective-C as well.
obj-c 名词的更多相关文章
- Web前端名词释义及原理
引言:看题目的时候,不要觉得这是一个很深奥的问题,Web前端这些东西很多就是叫的名字牛逼,其实原理很TM简单,也就那么回事. 一.javascript名词释义 1.啥是事件队列? 就是 弄一个数组,里 ...
- Python C/S架构,网络通信相关名词,socket编程
主要内容: 一. C/S架构 二. 网络通信的相关名词 三. socket编程 一. C/S架构和B\S架构概述 1. C/S架构: Client/Server(客户端/服务端)架构 描述: C/S ...
- vmware里面的名词 vSphere、vCenter Server、ESXI、vSphere Client
vmware里面的名词 vSphere.vCenter Server.ESXI.vSphere Client vSphere.vCenter Server.ESXI.vSphere Client VS ...
- obj.style.z-index的正确写法
obj.style.z-index的正确写法 今天发现obj.style.z-index在js里面报错,后来才知道在js里应该把含"-"的字符写成驼峰式,例如obj.style.z ...
- CSharpGL(9)解析OBJ文件并用CSharpGL渲染
CSharpGL(9)解析OBJ文件并用CSharpGL渲染 2016-08-13 由于CSharpGL一直在更新,现在这个教程已经不适用最新的代码了.CSharpGL源码中包含10多个独立的Demo ...
- nodejs、npm、grunt——名词解释
最近着手开发一个新项目,打算从工程化的角度整理一套自己的前端开发.发布体系. grunt这些工具,之前别人用我也用,并没有认真想过它们的前世今生,正好趁着这个机会,我来理一理目前业界比较流行这些工具的 ...
- BeanUtils.populate(obj, map);
public static void populate(Object bean, Map<String, ? extends Object> properties) throws Ille ...
- VC++ : error LNK2005: ... already defined in *.obj
今天写代码遇到了这么一个链接错误:"已经在*.obj中定义". error LNK2005: "void __cdecl ReplaceWstringVar(class ...
- 111. for(元素变量x:遍历对象obj)
package com.chongrui.test;/* * for(元素变量x:遍历对象obj){ * 引用X的java语句 * * } * * * */public class test { ...
- 让Git忽略所有obj和bin目录的同步
DotNet的项目里,编译出来的二进制文件默认都是放在每个项目对应的bin和obj目录下,有时候开发人员会不小心把这些目录的文件错误的提交到Git服务器.Git里的忽略文件功能可以通过一个.gitig ...
随机推荐
- 2016031901 - U盘安装ubuntu系统
使用U盘安装ubuntu系统 01.进入u盘安装 个人使用的是闪迪U盘安装 02. 个人重装ubuntu 03.进入ubuntu安装界面 04.进入语言界面 05.准备安装ubuntu 06.ubun ...
- gdb在Mac上的安装步骤
到gdb官网下载gdb安装包,解压后,在“终端”中进入其目录:然后依次执行如下命令行: 1st:CFLAGS='-Wno-string-plus-int -g -O2' ./configure --d ...
- 数据结构---顺序表(C++)
顺序表 是用一段地址连续的存储单元依次存储线性表的数据元素. 通常用一维数组来实现 基本操作: 初始化 销毁 求长 按位查找 按值查找 插入元素 删除位置i的元素 判空操作 遍历操作 示例代码: // ...
- ubuntu下的文本查看相关命令
文本查看 1.cat命令(查看文本内容) 使用时三种常用模式 (1)cat 文本名 直接查看文本内容 (2)cat 文本名 -n 直接查看文本内容,但为文本中所有行编号 (3)cat 文本名 -b 直 ...
- 几个.net的GUI控件库
https://github.com/firstfloorsoftware/mui http://wpftoolkit.codeplex.com/ https://github.com/fluentr ...
- C#如何控制方法的执行时间,超时则强制退出方法执行
转自:http://outofmemory.cn/code-snippet/1762/C-how-control-method-zhixingshijian-chaoshi-ze-force-quit ...
- 备忘·添加SublimeText3右键菜单
因为用的sublimeText3是免安装版,打开未关联文件,略有麻烦,所以搜了一下,发现几种解决办法,其中INF文件的方法很喜欢,备份下 [Version] Signature="$Wind ...
- Seven Python Tools All Data Scientists Should Know How to Use
Seven Python Tools All Data Scientists Should Know How to Use If you’re an aspiring data scientist, ...
- POJ 1364 King
http://poj.org/problem?id=1364 题意 :给出一个序列a1,a2,a3,a4.....ai,......at ;然后给你一个不等式使得ai+a(i+1)+a(i+2)+.. ...
- AVPicture、AVFrame和AVPacket
http://blog.csdn.net/ym012/article/details/6540065 从定义上可知,AVPicture是AVFrame的一个子集,他们都是数据流在编解过程中用来保存数据 ...