In Swift, can one use a string to access a struct property?

后端 未结 4 785
粉色の甜心
粉色の甜心 2021-01-24 15:53

I have a struct and I would like to know if I can access variables using bracket syntax. Here is my struct:

import UIKit

public struct Pixel {
    public var v         


        
4条回答
  •  不知归路
    2021-01-24 16:07

    What you're describing is not a Swift feature, unless you use subscripting (as vadian has explained). If you want something that supplies subscripting automatically, use a dictionary. Otherwise, if you really want true introspection, use Cocoa — use a class derived from NSObject instead of a struct, and employ key-value coding.

提交回复
热议问题