在Swift中按名称获取实例变量(Get instance variable by name in Swift)

编程入门 行业动态 更新时间:2024-10-26 01:22:19
在Swift中按名称获取实例变量(Get instance variable by name in Swift)

我试图在swift中复制此功能: https : //stackoverflow.com/a/11774276/4102858 。 我能够在Swift中成功重新创建allPropertyNames()。 但是,我在使用方法pointerOfIvarForPropertyNamed()时也遇到了同样的问题。 主要原因是Objc解决方案使用函数object_getInstanceVariable(),这在Swift中不可用。

TLDL:我试图在Swift中重新创建这个方法:

- (void *)pointerOfIvarForPropertyNamed:(NSString *)name { objc_property_t property = class_getProperty([self class], [name UTF8String]); const char *attr = property_getAttributes(property); const char *ivarName = strchr(attr, 'V') + 1; Ivar ivar = object_getInstanceVariable(self, ivarName, NULL); return (char *)self + ivar_getOffset(ivar); }

这就是我现在所处的位置:

func pointerOfIvarForPropertyNamed(name: String) -> AnyObject { func unicodeValueOfString(string: String) -> UInt32 { for s in String(string).unicodeScalars { return s.value } return 0 } var property: COpaquePointer = class_getProperty(self.classForCoder, (name as NSString).UTF8String) var attr: UnsafePointer<Int8> = property_getAttributes(property) var ivarName: UnsafeMutablePointer<Int8> = strchr(attributesPointer, Int32(unicodeValueOfString("V"))) + 1 //object_getInstanceVariable() not available in Swift }

问题:

函数object_getInstanceVariable()在Swift中不可用

我一般缺乏使用Objective-C运行时的经验

I am trying to replicate this functionality in swift: https://stackoverflow.com/a/11774276/4102858. I was able to successfully recreate allPropertyNames() in Swift. However, I'm having trouble doing the same with the method pointerOfIvarForPropertyNamed(). The main reason is that the Objc solution uses the function object_getInstanceVariable(), which isn't available in Swift.

TLDL: Im trying to recreate this method in Swift:

- (void *)pointerOfIvarForPropertyNamed:(NSString *)name { objc_property_t property = class_getProperty([self class], [name UTF8String]); const char *attr = property_getAttributes(property); const char *ivarName = strchr(attr, 'V') + 1; Ivar ivar = object_getInstanceVariable(self, ivarName, NULL); return (char *)self + ivar_getOffset(ivar); }

This is where I'm at now:

func pointerOfIvarForPropertyNamed(name: String) -> AnyObject { func unicodeValueOfString(string: String) -> UInt32 { for s in String(string).unicodeScalars { return s.value } return 0 } var property: COpaquePointer = class_getProperty(self.classForCoder, (name as NSString).UTF8String) var attr: UnsafePointer<Int8> = property_getAttributes(property) var ivarName: UnsafeMutablePointer<Int8> = strchr(attributesPointer, Int32(unicodeValueOfString("V"))) + 1 //object_getInstanceVariable() not available in Swift }

Problems:

The function object_getInstanceVariable() is unavailable in Swift

My general lack of experience using Objective-C runtime

最满意答案

来自Swift文档(强调我的):

如果您有使用Objective-C的经验,您可能知道它提供了两种方法来存储值和引用作为类实例的一部分。 除了属性之外,您还可以使用实例变量作为存储在属性中的值的后备存储。

Swift将这些概念统一到一个属性声明中。 Swift属性没有相应的实例变量,并且不直接访问属性的后备存储。 这种方法避免了在不同的上下文中如何访问值的混淆,并将属性的声明简化为单个明确的语句。 有关属性的所有信息(包括其名称,类型和内存管理特征)都在单个位置定义,作为类型定义的一部分。

根据如何使用pointerOfIvarForPropertyNamed ,您可能无法将该方法直接转换为Swift,因为它没有Ivars。 您可以使用键值编码来实现您想要的效果。

如果你想扩展这个方法的使用方法,我可以适当地扩展我的答案。

From the Swift docs (emphasis mine):

If you have experience with Objective-C, you may know that it provides two ways to store values and references as part of a class instance. In addition to properties, you can use instance variables as a backing store for the values stored in a property.

Swift unifies these concepts into a single property declaration. A Swift property does not have a corresponding instance variable, and the backing store for a property is not accessed directly. This approach avoids confusion about how the value is accessed in different contexts and simplifies the property’s declaration into a single, definitive statement. All information about the property—including its name, type, and memory management characteristics—is defined in a single location as part of the type’s definition.

Depending on how pointerOfIvarForPropertyNamed is used, you may not be able to literally translate that method into Swift, since it doesn't have Ivars. You may be able to use Key-Value Coding to achieve what you want.

If you want to expand on how this method is used, I can expand my answer appropriately.

更多推荐

本文发布于:2023-07-30 07:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336738.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   实例   名称   Swift   instance

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!