0

How can I have the same result by using the Split( ) method instead of componentsSeparatedByString( ) method:

public func className(obj: Any) -> String { return toString(obj.dynamicType).componentsSeparatedByString(".").last! } 

3 Answers 3

1

Try this:

// Swift 1.2 public func className(obj: Any) -> String { return split(toString(obj.dynamicType)) { $0 == "." }.last! } // Swift 2.0 public func className(obj: Any) -> String { return String(obj.dynamicType) .characters.split { $0 == "." } .map { String($0) } .last! } print(className(42)) // Int print(className("Hello world")) // String print(className(1.0)) // Double print(className([1,2,3])) // Array<Int> 
Sign up to request clarification or add additional context in comments.

Comments

1

Use return split(toString(obj.dynamicType)) { $0 == "." }.last!, although force-unwrapping last seems unwise.

Comments

0

After some digging I came up with this solution:

public func className(obj: Any) -> String { return split(toString(obj.dynamicType),maxSplit: Int.max,allowEmptySlices:false, isSeparator: { $0 == "."}).last! } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.