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.

时间: 2024-08-09 22:02:43

obj-c 名词的相关文章

OpenGL - obj文件的导入

http://blog.csdn.net/silangquan/article/details/9707347 Obj文件简介 OBJ文件是Alias|Wavefront公司为它的一套基于工作站的3D建模和动画软件"Advanced Visualizer"开发的一种标准3D模型文件格式,很适合用于3D软件模型之间的互导,也可以通过Maya读写.比如你在3dsMax或LightWave中建了一个模型,想把它调到Maya里面渲染或动画,导出OBJ文件就是一种很好的选择.目前几乎所有知名的3

有没有挂载分区这个名词

目前在学习linux系统的过程中,经常遇到如下的名词,启动分区,引导分区,根分区,挂载分区,交换分区,分别对应的挂载目录如下: /boot   /boot / /mnt   挂载分区不需要指定挂载点.实际的理解是:这些分区分别挂载在这些目录下,鸟哥的私房菜是如下的描述:创建/boot目录的分区.在实际的应用中,我们常常说创建启动分区等等,尤其是创建日志分区和挂载分区是否是正确,有没有错误?? 有没有挂载分区这个名词,布布扣,bubuko.com

OpenGL学习--07--模型加载(obj)

1.tutorial07.cpp // Include standard headers #include <stdio.h> #include <stdlib.h> #include <vector> // Include GLEW #include <GL/glew.h> // Include GLFW #include <glfw3.h> GLFWwindow* window; // Include GLM #include <glm

File类--System.out.print(Object obj)的理解

一.File 类(java.io) 概述:Java中使用File类来表示文件或者文件夹对象!     抽象路径名:描述文件或文件夹时,使用的路径符号,就是一个对象的字符串表示形式,如"c:\\";     绝对路径:绝对位置开始的路径;     相对路径:相对位置开始的路径; 构造方法:     File(String pathname)     File(String parent, String child)     File(File parent, String child)

JavaSE--【转】网络安全之证书、密钥、密钥库等名词解释

转载:http://www.cnblogs.com/alanfang/p/5600449.html 那些证书相关的名词解释(SSL,X.509,PEM,DER,CRT,CER,KEY,CSR,P12等) : http://www.360doc.com/content/15/0520/10/21412_471902987.shtmlKeystore介绍:http://blog.csdn.net/yangtao6888/article/details/796124使用CryptoAPI解析X509证

ArrayList集合--关于System.out.print(Object obj);的理解

1.ArrayList集合中常用的方法 ArrayList<Student> stuArrayList = new ArrayList<>(); //定义一个集合对象 stuArrayList.add():    //添加元素 stuArrayList.add(index, e):    //在某个位置添加元素,但不覆盖原元素 stuArrayList.get(index):    //获取某位置的元素 stuArrayList.size():    //获取集合长度 stuArr

$(obj).index(this)与$(this).index()异同讲解

$(this).index()在使用jQuery时出镜率非常高,在编写选项卡及轮播图等特效时经常用到,但$(obj).index(this)似乎有点陌生. 为便于理解,以下分两个使用场景加以分析. 场景一: 同级元素标签相同 <ul> <li>one</li> <li>two</li> <li>three</li> <li>four</li> </ul> <script> /

利用Object.prototype.toString.call(obj)判断数据类型

1.typeof判断数据类型 typeof可以用来判断基本数据类型,对于Array.Error.RegExp.Number.Function只能判断为object 2.使用Object.prototype.toString.call(obj)判断数据类型 var a=new Number(12); var toString=Object.prototype.toString; console.log(toString.call(a));//[object Number] console.log(

拿到内存中dom元素的最后样式进行修改obj下的currentStyle方法

在用dom操作在对页面中的<style></style>里的样式进行操作时,发现时无效的,我觉得是因为页面DOM解析时此标签的样式内容才会被读到内存中,因此JS操作时取不到此标签的内容 一个标签的样式有可能是多个样式文件所定义而成了,所以浏览器在解释标签的时候会有一个计算标签最后样式的过程,想要对<style>中定义格式的标签的样式进行修改必须去操作最后浏览器计算机计算出的样式,这里用obj下的currentStyle方法,具体测试如下: <html> &l

前端名词收集

收集于互联网 update at 2016年10月02日19:40:25 IIFE ("Immediately Invoked Function Expression") (function(param){ })(params); shim和polyfill shim的意思是在一个老(旧)环境中模拟出一个新API,有时也叫做shiv,例如著名的html5shiv库. polyfill的意思和shim差不多,2010年10月份Remy Sharp引进了这个概念,一个polyfill是一段