热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

星号*在Objective-C中是什么意思?-WhatdoestheAsterisk*meaninObjective-C?

Isittrue,thattheAsteriskalwaysmeansHey,thatisapointer!AndanPointeralwaysholdsan

Is it true, that the Asterisk always means "Hey, that is a pointer!" And an Pointer always holds an memory adress?

星号总是表示“嘿,那是一个指针!”指针总是包含一个内存地址?

(Yes I know for the exception that a * is used for math operation)

(是的,我知道一个*用于数学运算的例外情况)

For Example:

例如:

NSString* myString;

or

SomeClass* thatClass;

or

(*somePointerToAStruct).myStructCompOnent= 5;

I feel that there is more I need to know about the Asterirsk (*) than that I use it when defining an Variable that is a pointer to a class.

我觉得在定义一个指向类的变量时,我需要知道的关于星号(*)的更多信息。

Because sometimes I already say in the declaration of an parameter that the Parameter variable is a pointer, and still I have to use the Asterisk in front of the Variable in order to access the value. That recently happened after I wanted to pass a pointer of an struct to a method in a way like [myObj myMethod:&myStruct], I could not access a component value from that structure even though my method declaration already said that there is a parameter (DemoStruct*)myVar which indeed should be already known as a pointer to that demostruct, still I had always to say: "Man, compiler. Listen! It IIISSS a pointer:" and write: (*myVar).myStructCompOnentX= 5;

因为有时候我已经在参数声明中说过参数变量是指针,而且我还必须在变量前面使用星号来访问值。最近发生在我想要一个结构体的指针传递给一个方法在某种程度上像[myObj myMethod:&myStruct],我不能访问组件值结构尽管我的方法声明已经说过有一个参数(DemoStruct *)myvar#的确应该已经称为一个指针DemoStruct,仍然我总是说:“男人,编译器。听!它就像一个指针:“并且写:(*myVar)。myStructCompOnentX= 5;

I really really really do not understand why I have to say that twice. And only in this case.

我真的真的不明白为什么我得说两遍。只有在这种情况下。

When I use the Asterisk in context of an NSString* myString then I can just access myString however I like, without telling the compiler each time that it's a pointer. i.e. like using *myString = @"yep".

当我在NSString* myString上下文中使用星号时,我可以随意访问myString,而不用每次都告诉编译器它是一个指针。例如使用*myString = @" yes "。

It just makes no sense to me.

这对我来说毫无意义。

4 个解决方案

#1


24  

an * is actually an operator to de-reference a pointer. The only time it means "hey i'm a pointer" is during variable declaration.

*实际上是一个去引用指针的操作符。它唯一表示“嘿,我是一个指针”的时间是在变量声明期间。

Foo* foo  // declare foo, a pointer to a Foo object
&foo      // the memory address of foo
*foo      // de-reference the pointer - gives the Foo object (value)

#2


4  

mmattax well covered the distinction between declaration (as a pointer) and dereferencing.

mmattax很好地描述了声明(作为指针)和取消引用之间的区别。

However, as to your point about:

然而,关于你的观点:


  (*myVar).myStructCompOnentX= 5;

to access a member of an instance of a C struct (as this is) you can do what you did , or more commonly you use the -> notation:

要访问C struct实例的成员(这是),您可以做您所做的,或者更常见的是使用->表示法:


  myVar->myStructCompOnentX= 5;

Objective-C is a little confusing here because it recently (in ObjC 2.0) introduced property syntax, which is a short cut for:

Objective-C有点让人困惑,因为它最近(在c 2.0中)引入了属性语法,这是一个捷径:


  int val = [myObject someIntProperty];

and can now be written as:

现在可以写成:


  int val = myObject.someIntProperty;

This is Objective C (2.0) syntax for accessing a property which you have declared (not an actual member variable), whereas your example was accessing a member of a C struct.

这是目标C(2.0)语法,用于访问已声明的属性(不是实际的成员变量),而您的示例是访问C结构体的成员。

Make sure you are clear on the difference.

一定要清楚区别。

#3


1  

For other people who are new to Objective-C like me, this tutorial was really helpful in understanding the asterisk and other basics:

对于像我这样对Objective-C不熟悉的人来说,本教程对理解星号和其他基础知识非常有帮助:

The Beginner’s Guide to Objective-C: Language and Variables

Objective-C的初学者指南:语言和变量。

#4


0  

As I said in my answer of your previous question, @"yep" is already a pointer, so there is no need of * before myString which is also a pointer. In this case, you assign pointers not values.

正如我在回答你之前的问题时说的,@“是的”已经是一个指针,所以在myString之前不需要*,它也是一个指针。在这种情况下,您分配的是指针而不是值。


推荐阅读
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 本文介绍了iOS数据库Sqlite的SQL语句分类和常见约束关键字。SQL语句分为DDL、DML和DQL三种类型,其中DDL语句用于定义、删除和修改数据表,关键字包括create、drop和alter。常见约束关键字包括if not exists、if exists、primary key、autoincrement、not null和default。此外,还介绍了常见的数据库数据类型,包括integer、text和real。 ... [详细]
  • 本文介绍了在处理不规则数据时如何使用Python自动提取文本中的时间日期,包括使用dateutil.parser模块统一日期字符串格式和使用datefinder模块提取日期。同时,还介绍了一段使用正则表达式的代码,可以支持中文日期和一些特殊的时间识别,例如'2012年12月12日'、'3小时前'、'在2012/12/13哈哈'等。 ... [详细]
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • JavaSE笔试题-接口、抽象类、多态等问题解答
    本文解答了JavaSE笔试题中关于接口、抽象类、多态等问题。包括Math类的取整数方法、接口是否可继承、抽象类是否可实现接口、抽象类是否可继承具体类、抽象类中是否可以有静态main方法等问题。同时介绍了面向对象的特征,以及Java中实现多态的机制。 ... [详细]
  • 本文介绍了Oracle数据库中tnsnames.ora文件的作用和配置方法。tnsnames.ora文件在数据库启动过程中会被读取,用于解析LOCAL_LISTENER,并且与侦听无关。文章还提供了配置LOCAL_LISTENER和1522端口的示例,并展示了listener.ora文件的内容。 ... [详细]
author-avatar
sunhuan
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有