2015-11-02 5 views
6

पर गुण follownig साथ UIView या UIViewController के सभी गुण प्राप्त करने की कोशिश कर रहा:स्विफ्ट मिरर प्रतिबिंब वापस नहीं UIView

func propertysNames()->[String]{ 
    var s = [String]() 
    for c in Mirror(reflecting: self).children 
    { 
     if let name = c.label{ 
      s.append(name) 
     } 
    } 
    return s 
} 

यह UIViewController पर काम करता है, लेकिन UIView, गुण वापस जाने के लिए कोई सलाह नहीं लगता है?

उत्तर

5

सुनिश्चित नहीं हैं कि आप क्या हासिल करने की कोशिश कर रहे हैं, लेकिन UIView NSObject विरासत में मिला। इस वजह से, आपके पास अपने निपटारे में ओबीजेसी रनटाइम का अधिकतर हिस्सा है। तो एक विकल्प के रूप, आप निम्न कर सकता है:

import UIKit 

extension NSObject { 
    func propertysNames() -> [String]{ 
    var count : UInt32 = 0 
    let classToInspect = self.dynamicType 
    let properties : UnsafeMutablePointer <objc_property_t> = class_copyPropertyList(classToInspect, &count) 
    var propertyNames : [String] = [] 
    let intCount = Int(count) 
    for var i = 0; i < intCount; i++ { 
     let property : objc_property_t = properties[i] 
     let propertyName = NSString(UTF8String: property_getName(property))! 
     propertyNames.append(propertyName as String) 
    } 
    free(properties) 
    return propertyNames 
    } 
} 

print(UIView().propertysNames()) 
// prints: "["_mayRemainFocused", "_sensitivitySize", "skipsSubviewEnumeration", "viewTraversalMark", "viewDelegate", "monitorsSubtree", "backgroundColorSystemColorName", "currentScreenScale", "maskView", "_userInterfaceIdiom", "hash", "superclass", "description", "debugDescription", "gesturesEnabled", "deliversTouchesForGesturesToSuperview", "deliversButtonsForGesturesToSuperview", "_shouldReverseLayoutDirection", "leadingAnchor", "trailingAnchor", "leftAnchor", "rightAnchor", "topAnchor", "bottomAnchor", "widthAnchor", "heightAnchor", "centerXAnchor", "centerYAnchor", "firstBaselineAnchor", "lastBaselineAnchor", "_keyboardOrientation", "_touchForceObservable", "_inheritedRenderConfig", "_lightStyleRenderConfig", "_accessoryViewFrame", "unsatisfiableConstraintsLoggingSuspended", "hash", "superclass", "description", "debugDescription", "hash", "superclass", "description", "debugDescription", "userInteractionEnabled", "tag", "layer", "focused", "semanticContentAttribute", "interactionTintColor", "_layoutDebuggingIdentifier", "_countOfMotionEffectsInSubtree", "_maskView", "_ancestorDefinesTintColor", "_ancestorDefinesTintAdjustmentMode", "_presentationControllerToNotifyOnLayoutSubviews", "_rawLayoutMargins", "_inferredLayoutMargins", "_dontUpdateInferredLayoutMargins", "_tracksFocusedAncestors", "_countOfFocusedAncestorTrackingViewsInSubtree", "_mutableLayoutGuides", "_mutableLayoutArrangements", "_hiddenManagedByLayoutArrangementCount", "_pendingHiddenCount", "previewingSegueTemplateStorage", "_continuousCornerRadius", "_canBeParentTraitEnviroment", "_layoutEngine", "_boundsWidthVariable", "_boundsHeightVariable", "_minXVariable", "_minYVariable", "_internalConstraints", "_constraintsExceptingSubviewAutoresizingConstraints", "unsatisfiableConstraintsLoggingSuspended", "_shouldArchiveUIAppearanceTags", "_interactionTintColor", "_backdropMaskViewForGrayscaleTint", "_backdropMaskViewForColorTint", "_backdropMaskViewForFilters", "_backdropMaskViews", "_wantsGeometryChangeNotification", "contentSizeNotificationToken", "layoutMarginsGuide", "readableContentGuide", "hash", "superclass", "description", "debugDescription", "traitCollection", "preferredFocusedView", "center", "bounds", "transform", "collisionBoundsType", "collisionBoundingPath"]\n" 

इसके अलावा, मैं देख रहा हूँ कुछ weirdness वस्तुओं UIKit करने के लिए अपने कोड लागू करते हैं। निश्चित नहीं है कि चर क्या है जिससे यह असफल हो जाता है। स्विफ्ट में लिखा NSObject प्रकार पर ठीक से काम करने लगता है:

import UIKit 

class Fruit { 
    var type=1 
    var name="Apple" 
    var delicious=true 
} 

var s = [String]() 
for c in Mirror(reflecting: Fruit()).children 
{ 
    if let name = c.label{ 
    s.append(name) 
    } 
} 

print(s) 
// works: "["type", "name", "delicious"]\n" 

class FruitNSObject: NSObject { 
    var type:NSNumber=1 
    var name:NSString="Apple" 
    var delicious=true 
} 

s = [String]() 
for c in Mirror(reflecting: FruitNSObject()).children 
{ 
    if let name = c.label { 
    s.append(name) 
    } 
} 

print(s) 
// works: "["type", "name", "delicious"]\n" 

s = [String]() 
for c in Mirror(reflecting: UIView()).children 
{ 
    if let name = c.label { 
    s.append(name) 
    } 
} 

print(s) 
// doesn't work: "[]\n" 

s = [String]() 
for c in Mirror(reflecting: UIViewController()).children 
{ 
    if let name = c.label { 
    s.append(name) 
    } 
} 

print(s) 
// doesn't work: "[]\n" 

तो या तो यह एक बग है या वहाँ स्विफ्ट < में कुछ सीमा है -> ObjC स्विफ्ट के वर्तमान संस्करण में। शायद इसे @ user3441734 ने अपने उत्तर में बताया है कि क्या करना है।

बीटीडब्ल्यू, सभी कोड एक खेल के मैदान में एक्सकोड (7.1.1) के वर्तमान संस्करण पर चलाया गया था।

+0

महान उत्तर के लिए धन्यवाद! इसके साथ समस्या यह है कि आप उदास हैं कि यह केवल एनएसओब्जेक्ट्स विरासत पर ही काम करता है, और मैं कुछ जेनेरिक चुड़ैल किसी पर काम करेगा। इसके अलावा (और मैं इस बारे में 100% निश्चित नहीं हूं) इस तरह स्विफ्ट के रास्ते में थोड़ा 'भारी' लगता है .. मैं आपका जवाब स्वीकार करता हूं क्योंकि यह सबसे अच्छा है। एक बार फिर धन्यवाद। – MCMatan

4
import UIKit 

let viewController = UIViewController() 
let view = UIView() 
class MyViewController: UIViewController { 
    let i = 1 
    let myView = MyView() 
} 
class MyView : UIView { 
    let label = UILabel() 
    let i = 1 
} 

Mirror(reflecting: MyViewController()).children.count // 2 
Mirror(reflecting: MyView()).children.count    // 0 

आप सही हैं! बस बग रिपोर्ट को भरने ...

या इसे तोड़-फोड़ है, क्योंकि मैं इस

extension UIView : _Reflectable { 
} 
+0

यह निश्चित रूप से "तबाही" है, मैं सहमत हूं। –

संबंधित मुद्दे