I am learning swift and trying to know all the loops and know about it.
This objective is trying to port the code into Swift.
NSInteger sort (id num1, id num2, void * context) {int v1 = [num1 intValue]; Int v2 = [num2 intValue]; If sending (v1 and lieutenant; v2) NSordadding; Else if NSOrderedDescending (v1> v2); Others will return NSOrdered; }
This is what I have not yet done.
Function type (num1: AnyObject, num2: AnyObject, context: zero) - & gt; NSInteger {var v1: Int = num1.integerValue var v2: Int = num2.integerValue if (v1 and lt; v2) {return nsord ordering} and if (v1> v2) {return NSOrderedDescending} else {return NSOrderedSame}}
There is a problem with this method NSOrderedAscending is not in existence and I believe that I have the legal name all wrong. Any tips or suggestions are appreciated.
There are issues that do not despise a straightforward harbor.
For one thing, the object-C code says the intValue
method on the object. This indicates that the object to be passed to the function is expected to be the NSString
example, so the swift function should actually take the NSString
argument, not AnyObject
argument (unless the function actually can not take any type of object, see the note below in that case). Also, the reference
argument is not being used at all, so it is not really necessary. Also, the type of return is actually nscimpression result
, and not NSInteger
. Given this, the Swift version will look something like this:
Fun sort (num1: NSString, num2: NSString) -> NSComparisonResult {v1 = num1.intValue v2 = num2.intValue if v1 & lt; V2 {return NSComparisonResult.OrderedAscending} and if v1 == v2 {NSComparisonResult.Same} and {NSComparisonResult.OrderedDescending}}
If SWIFT version actually does Need to work on any type of object, it would be better to use conditional artists:
Joy sort (num1: AnyObject, num2: AnyObject) -> ; NSComparisonResult {if let's n1 = num1? NSString {if let's say n2 = num2? NSString {let v1 = n1.intValue v2 = n2.intValue if v1 & lt; V2 {return NSComparisonResult.OrderedAscending} and if v1 & gt; V2 {return NSComparisonResult.OrderedDescending}}} NSComparisonResult.Same}
No comments:
Post a Comment