-2

I examined that question: How do I print the type or class of a variable in Swift?

But still i cant find answer, how to output class of object in Swift?

In Obj-C that was simple - [myClass class]; but in swift i can't understand how to do that.

For example, consider following:

 let coordinates = (x: 3, y: 2, z: 5)
print(type(of: coordinates))

That output:"(Int, Int, Int)\n"

But i dont want to know what is inside, i want to know class name of coordinates! What is that? Tuple, an array or dictionary?

Can someone provide code snippet to explain that? Thanks!

Community
  • 1
  • 1
Evgeniy Kleban
  • 6,794
  • 13
  • 54
  • 107
  • 2
    `coordinates` doesn't have a class. It is a tuple and its type is `(Int, Int, Int)` as shown. – Paulw11 Feb 04 '17 at 12:02
  • 1
    First of all, it's a struct not a class. Secondly, the type is a tuple of three `Int`s, what's wrong with the result? – vadian Feb 04 '17 at 12:02
  • @vadian why is that not a class? What if i want to define Array class like that or dictionary? Why swift decide for me whether i want create class or struct? – Evgeniy Kleban Feb 04 '17 at 12:04
  • 3
    swift doesn't "decide"; if you declare a class it is a class. If it declare a struct it is a struct. Intrinsic types such as Int, tuples and collections such as arrays and dictionaries are value types. – Paulw11 Feb 04 '17 at 12:07
  • @Paulw11 if i declare let X = @"123" isn't that a class String with value of 123 inside? – Evgeniy Kleban Feb 04 '17 at 12:09
  • 3
    @EvgeniyKleban String is a struct not a class – Leo Dabus Feb 04 '17 at 12:11
  • @LeoDabus isnt that equivalent of NSString class in Obj-C? – Evgeniy Kleban Feb 04 '17 at 12:12
  • 1
    @EvgeniyKleban Similar but not the same. Can be converted to each other automatically but not the same. – Sulthan Feb 04 '17 at 12:12
  • @EvgeniyKleban [myClass class] in Swift would be `class MyClass { ... }` – Leo Dabus Feb 04 '17 at 12:13
  • @LeoDabus let arr: Array = [1, 2, 3] class.arr produce an error as well – Evgeniy Kleban Feb 04 '17 at 12:16
  • Swift is a type inferred language, most of the time you don't need to tell it the type of your objects. `let arr = [1, 2, 3]` What is `class.arr` ? – Leo Dabus Feb 04 '17 at 12:17
  • @LeoDabus nothing, it product an error. Try it in playground please. – Evgeniy Kleban Feb 04 '17 at 12:19
  • You need to specify the type of your array or let Swift infer the type which is preferred. `let arr: Array = [1, 2, 3]` or `let arr = [1, 2, 3]` – Leo Dabus Feb 04 '17 at 12:20
  • @LeoDabus good, i still cant get result from class.arr – Evgeniy Kleban Feb 04 '17 at 12:21
  • 2
    `type(of: arr)` Why do you need that ? – Leo Dabus Feb 04 '17 at 12:22
  • @LeoDabus thank you, but really, is there is separate struct called Array and NSArray class in Swift? – Evgeniy Kleban Feb 04 '17 at 12:24
  • 1
    @EvgeniyKleban You don't need to use NSArray in Swift. You should use Array only. – Leo Dabus Feb 04 '17 at 12:26
  • And if arr is a property of your MyClass `class MyClass { let arr = [1, 2, 3] } type(of: MyClass().arr)` or if it is a static property `class MyClass { static let arr = [1, 2, 3] } type(of: MyClass.arr)` – Leo Dabus Feb 04 '17 at 12:26
  • @LeoDabus why should i prefer to use struct over NSObject class? – Evgeniy Kleban Feb 04 '17 at 12:47
  • The question is Why do you need to subclass NSObject? If you don't use a struct. You don't need to create initializers for a struct. – Leo Dabus Feb 04 '17 at 12:54
  • @LeoDabus because objects have usefull class methods that it inherit from super class? For example for NSArray - count, object at index, etc.? – Evgeniy Kleban Feb 04 '17 at 12:55
  • You have it all with a struct. What do you mean ? The only case I see you would need to use a class would be subclassing or to make it persist making it NSCoding compliant. BTW Swift is a protocol oriented language. – Leo Dabus Feb 04 '17 at 12:56
  • @EvgeniyKleban You really should learn more about the benefits of value types and what you can do with a struct in Swift. Please read the [related section](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/ClassesAndStructures.html#//apple_ref/doc/uid/TP40014097-CH13-ID82) in the Swift Programming Language manual. – Luca Angeletti Feb 04 '17 at 13:02

1 Answers1

2

Tuple is a compound type and compound types don't have names. You can get the types of its elements as you said. So your code in your question is the right way to get the type of variable. For example, code below will print the type of dictionary.

let someDictionary = ["Alex": 31, "Paul": 39]
print(type(of: someDictionary))

Output : Dictionary < String, Int >

You can have more detailed information about types from Swift Programming Language Reference.