2014-09-29 10 views
3

데이터 TypeSwift language에 다시 가져올 수 있습니까?클래스에서 유형 가져 오기

class Test : NSObject 
{ 
    let field1 : String 
    let field2 : Int 

    init(value1 : String, value2 : Int) 
    { 
     field1 = value1 
     field2 = value2 
    } 
} 

let test1 = Test(value1: "Hello", value2: 1) 

//this code return the same string with class name (__lldb_expr_129.Test) 
let classString = NSStringFromClass(test1.classForCoder) 
let className2 = reflect(test1).summary 

//this code return the same ExistentialMetatype value and not the Tes or NSObject type 
let classType = reflect(test1).valueType 
let classType2 = test1.self.classForCoder 

//this return Metatype and not the Test or NSObject type 
let classType3 = test1.self.dynamicType 

Test 형 아닌 ExistentialMetatypeMetatype 또는 값을 가져 오지하는 방법이된다

는 일례이다?

+0

가능한 복제본 [Swift에서 변수의 유형이나 클래스를 어떻게 인쇄합니까?] (http://stackoverflow.com/questions/24006165/how-do-i-print-the-type-or) -class-of-a-variable-in-swift) –

답변

1
  1. .self은 무의미합니다. test1.selftest1
  2. test1.classForCoder과 같습니다. test1.dynamicType은 단지 Test.self입니다. 이것은 유형 (메타 유형 유형의 값)입니다. 스위프트 타입은 현재 인쇄 가능한 표현이 좋지 않습니다. 좋은 인쇄가 아니기 때문에 그것이 원하는 것이 아닙니다. 이 경우 유형이 클래스이므로 NSStringFromClass을 사용하거나 객체에 캐스팅하여 (Objective-C 클래스 객체 제공) 인쇄 할 수 있습니다.
+0

C# – PizzaRings

+0

@PizzaRings와 같이 메소드'objcet.getType()'이 있는지 궁금합니다. ** PizzaRings가 당신에게주었습니다. ** 메소드가 필요 없습니다. **. 너는 읽었습니까 (2.). 당신은 클래스 *의 타입을'Test.self'로 원했습니다. – Binarian

+0

@ 피자 링스 : 예. 'object.dynamicType' 당신에게 유형을 제공합니다. 거기에 아무 문제가 없습니다. 인쇄 할 때 아무 것도 보이지 않습니다. – newacct