A quick tip this week. If you have been using the object_getClassName()
function when using Objective C to print out the class of an object, you can use dynamicType
to do it in Swift:
var s = "abc"
println("class name: \(s.dynamicType)")
In addition, you can use dynamicType
to create a new instance of the class:
var type = s.dynamicType
var anotherInstance = type() //Like String()
Your feedback is valuable: Do you want more nuggets like this? Yes or No
.
Here's a few related nuggets:
- iOS Dev Nugget 314 Checked Array Subscripting
- iOS Dev Nugget 310 Weak Reference Wrapper Class
- iOS Dev Nugget 308 Careful with early return statements in functions that return void
- iOS Dev Nugget 307 Array Out of Bounds
- iOS Dev Nugget 306 Underscores in Numeric Literals
- iOS Dev Nugget 303 Use Enums for States
- iOS Dev Nugget 301 Print App Document Path II
- iOS Dev Nugget 300 Pipe Operator
- iOS Dev Nugget 292 for-where
- iOS Dev Nugget 291 Implicit Member Expressions
- iOS Dev Nugget 287 Modifying a Property Which is a Struct Triggers didSet
- iOS Dev Nugget 265 value type cannot have a stored property that recursively contains it
- iOS Dev Nugget 257 Asserts
- iOS Dev Nugget 251 Retrieving the Current View Controller
- iOS Dev Nugget 246 Mapping Swift and Obj-C Method Names
- iOS Dev Nugget 244 IteratorProtocol
- iOS Dev Nugget 241 Swift Enums with Labels
- iOS Dev Nugget 240 Print an Object's Unique Identifier
- iOS Dev Nugget 234 #available and @available
- iOS Dev Nugget 215 dump Instead of print for Swift Structs
- iOS Dev Nugget 211 Higher Order Functions: map, filter, reduce
- iOS Dev Nugget 209 Swift Defer
- iOS Dev Nugget 207 Swift Nil-Coalescing Operator
- iOS Dev Nugget 206 Date doesRelativeDateFormatting
- iOS Dev Nugget 205 The Never Return Type in Swift
- iOS Dev Nugget 204 Swift @autoclosure
- iOS Dev Nugget 203 Swift Measurement
- iOS Dev Nugget 202 Swift Enumerations to Encode States
- iOS Dev Nugget 201 Swift Pattern Matching
- iOS Dev Nugget 200 Swift flatMap
- iOS Dev Nugget 199 Swift Talk Video Series
- iOS Dev Nugget 198 KZPlayground
- iOS Dev Nugget 197 App Initialization Code
- iOS Dev Nugget 196 Rename Refactoring for Swift Code
- iOS Dev Nugget 193 Result Enumeration
- iOS Dev Nugget 188 SwiftLint
- iOS Dev Nugget 187 Creating a Singleton Class in Swift
- iOS Dev Nugget 186 Injection for Xcode
- iOS Dev Nugget 182 Tracking the swift-evolution Git Repository
- iOS Dev Nugget 181 Swift Selectors
- iOS Dev Nugget 180 Low Power Mode
- iOS Dev Nugget 172 Print App Document Path
- iOS Dev Nugget 168 Using Blocks to Manage Contexts
- iOS Dev Nugget 158 Checking if Newer APIs Are Available In Older iOS Versions (II)
- iOS Dev Nugget 149 Timepiece, a Swift Library for Handling Dates
- iOS Dev Nugget 143 Don't Forget SQLite
- iOS Dev Nugget 142 Grand Central Dispatch
- iOS Dev Nugget 141 Great Swift Standard Library Examples Covered in Playground (Swift 2.0)
- iOS Dev Nugget 138 Interoperability: Using Enumerations Defined In Objective-C from Swift
- iOS Dev Nugget 137 Named Tuple Elements
- iOS Dev Nugget 136 Auto Layout with Cartography
- iOS Dev Nugget 135 CAShapeLayer, Animated Views and XCPShowView()
- iOS Dev Nugget 133 Dollar.swift
- iOS Dev Nugget 132 XCPCaptureValue in Swift Playgrounds
- iOS Dev Nugget 131 Running Asynchronous Code in Swift Playgrounds
- iOS Dev Nugget 129 Browsing the Swift Standard Library
- iOS Dev Nugget 124 performSelector:withObject:afterDelay: in Swift
- iOS Dev Nugget 116 method() is unavailable: use object construction Class()
.