类:     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 名词的更多相关文章

  1. Web前端名词释义及原理

    引言:看题目的时候,不要觉得这是一个很深奥的问题,Web前端这些东西很多就是叫的名字牛逼,其实原理很TM简单,也就那么回事. 一.javascript名词释义 1.啥是事件队列? 就是 弄一个数组,里 ...

  2. Python C/S架构,网络通信相关名词,socket编程

    主要内容: 一.  C/S架构 二. 网络通信的相关名词 三. socket编程 一. C/S架构和B\S架构概述 1. C/S架构: Client/Server(客户端/服务端)架构 描述: C/S ...

  3. vmware里面的名词 vSphere、vCenter Server、ESXI、vSphere Client

    vmware里面的名词 vSphere.vCenter Server.ESXI.vSphere Client vSphere.vCenter Server.ESXI.vSphere Client VS ...

  4. obj.style.z-index的正确写法

    obj.style.z-index的正确写法 今天发现obj.style.z-index在js里面报错,后来才知道在js里应该把含"-"的字符写成驼峰式,例如obj.style.z ...

  5. CSharpGL(9)解析OBJ文件并用CSharpGL渲染

    CSharpGL(9)解析OBJ文件并用CSharpGL渲染 2016-08-13 由于CSharpGL一直在更新,现在这个教程已经不适用最新的代码了.CSharpGL源码中包含10多个独立的Demo ...

  6. nodejs、npm、grunt——名词解释

    最近着手开发一个新项目,打算从工程化的角度整理一套自己的前端开发.发布体系. grunt这些工具,之前别人用我也用,并没有认真想过它们的前世今生,正好趁着这个机会,我来理一理目前业界比较流行这些工具的 ...

  7. BeanUtils.populate(obj, map);

    public static void populate(Object bean, Map<String, ? extends Object> properties) throws Ille ...

  8. VC++ : error LNK2005: ... already defined in *.obj

    今天写代码遇到了这么一个链接错误:"已经在*.obj中定义". error LNK2005: "void __cdecl ReplaceWstringVar(class ...

  9. 111. for(元素变量x:遍历对象obj)

    package com.chongrui.test;/* * for(元素变量x:遍历对象obj){ * 引用X的java语句 *  * } *  *  * */public class test { ...

  10. 让Git忽略所有obj和bin目录的同步

    DotNet的项目里,编译出来的二进制文件默认都是放在每个项目对应的bin和obj目录下,有时候开发人员会不小心把这些目录的文件错误的提交到Git服务器.Git里的忽略文件功能可以通过一个.gitig ...

随机推荐

  1. 2014年度辛星css教程夏季版第三节

    第二节我们讲述的几乎全是CSS的选择器,那么下面这一节我们来讲一下CSS的颜色和文本的一些东西,虽然我对调色不大敏感,但是对于颜色还是比较感兴趣的. *********CSS中的颜色********* ...

  2. initialize or clean up your unittest within .net unit test

    // Use ClassInitialize to run code before running the first test in the class [ClassInitialize()] pu ...

  3. 使用WiX Toolset创建.NET程序发布Bootstrapper(安装策略管理)(一)-----初识WiX (转)

    原文地址:http://blog.csdn.net/rryqsh/article/details/8274832 Visual Studio 打包安装七宗罪 开发.NET的人,肯定会使用Visual ...

  4. BZOJ 3971 Матрёшка 解题报告

    很自然想到区间 DP. 设 $Dp[i][j]$ 表示把区间 $[i, j]$ 内的套娃合并成一个所需要的代价,那么有: $Dp[i][i] = 0$ $Dp[i][j] = min\{Dp[i][k ...

  5. Maven引入依赖后自动下载并关联源码(Source)

    好多用 Maven 的时候会遇到这样一个棘手的问题: 就是添加依赖后由于没有下载并关联源码,导致自动提示无法出现正确的方法名,而且不安装反编译器的情况下不能进入方法内部看具体实现 . 其实 eclip ...

  6. Visual Studio 创建代码注释默认模版方法

    在日常的开发中我们经常需要为页面添加注释和版权等信息,这样我们就需要每次去拷贝粘贴同样的文字,为了减少这种重复性的工作,我们可以把这些信息保存在Visual Studio 2012类库模版文件里 1. ...

  7. poj1177Picture(线段树-周长并)

    链接 神奇的扫描线啊 估计之前刷面积并的时候太急了 没来得及理解 .. 有一大段代码是与面积并一模一样的 都是离散化 更新 面积并是扫描的x  这次也一样 因为周长只算外围的 所以扫描到一条x边时 要 ...

  8. usaco4.12Fence Rails(迭代加深)

    为了这题还去学了下迭代加深 回来还是不会写 只好参考各大神的代码及题解了 二分枚举最大可以切的块数 然后就是各种分析及优化 USACO题解里写了7个优化.. 问题分析 抽象一下就可以发现,算法的本质是 ...

  9. poj1988

    知道了并查集写的问题后,我也明白了为什么之前这道题TLE的原因: 有这道题的合并操作不难想到用并查集维护: 由于并查集易于向上查询而不易于向下查询 所以对于询问方块x下面有多少个方块,我们可以转化为立 ...

  10. .NET System.Timers.Timer的原理和使用(开发定时执行程序)

    概述(来自MSDN) Timer 组件是基于服务器的计时器,它使您能够指定在应用程序中引发Elapsed 事件的周期性间隔.然后可以操控此事件以提供定期处理.例如,假设您有一台关键性服务器,必须每周7 ...