bridgeToObjectiveC not available on Swift Beta 5
Use as
to cast to NSString
for the same effect:
("string" as NSString).localizedCaseInsensitiveCompare("other string")
Or like this with optional chaining:
("string" as NSString?)?.localizedCaseInsensitiveCompare("other string")
bridgeToObjectiveC() not available for Array in Swift
Note that Swift has a 'find' function that you can use to see if an element is in an array or not:
find(array,element):C.Index?
It will return nil
if the item is not found. You should probably use this instead of contains.
bridgeToObjectiveC and makeObjectsPerformSelector in Swift beta 5
The bridgeToObjectiveC
and bridgeFromObjectiveC
functions are not available in Xcode 6.0 beta 5. Instead, cast to/from the appropriate Foundation type when you need to use that type's API on a Swift object. For example:
var arr = ["One", "Two"]
(arr as NSArray).indexOfObject("One")
Apple has warned against (or explicitly made unavailable) using performSelector
and related methods since the first Swift beta. Presumably any such API that were still available before beta 5 were unintentionally so.
As the question you cited notes, you can use map
to call a function/method on every element of an array. You can also use filter
, find
or a for
-in
loop, or after casting to NSArray
, one of the enumerateObjects
methods. Note that many consider it bad style to use the functional-programming constructs (map
, filter
, reduce
, find
) for tasks that aren't "functional" -- that is, to run code that has side effects. So a for
-in
loop might be the cleanest way to do what you're after.
String to float conversion in Swift Beta 5
floatValue
is a method of NSString
.Since bridgeToObjectiveC()
is no longer available with Xcode 6 Beta 5, you can use the as
operator to cast your Swift String
to NSString
:
(device.systemVersion as NSString).floatValue
removal of bridgeToObjective-C means I can't use NSRange
Here is a func that will take an NSRange
and replace a portion of a String
:
func replaceRange(nsRange:NSRange, #ofString:String, #withString:String) ->String {
let start = nsRange.location
let length = nsRange.length
let endLocation = start + length
let ofStringLength = countElements(ofString)
if start < 0 || endLocation < start || endLocation > ofStringLength {
return ofString
}
var startIndex = advance(ofString.startIndex, start)
var endIndex = advance(startIndex, length)
var range = Range(start:startIndex, end:endIndex)
var final = ofString.stringByReplacingCharactersInRange(range, withString:withString)
return final
}
var original = "This is a test"
var replacement = "!"
var nsRange:NSRange = NSMakeRange(1, 2)
var newString = replaceRange(nsRange, ofString:original, withString:replacement)
println("newString:\(newString)")
Output:
newString:!his is a test
NSString does not have a member named bridgeToObjectiveC
bridgeToObjectiveC()
was removed in Xcode 6 Beta 5. Now bridging is transparent, simply use
if urlContent.containsString("<span class=\"phrase\">") {
...
}
‘CGFloat’ is not convertible to ‘UInt8' and other CGFloat issues with Swift and Xcode 6 beta 4
Since Swift doesn't have implicit type conversions, you must specify all the type conversions that take place. What makes this case particularly tedious, is that currently Swift seems to lack direct conversions between CGFloat
and types such as UInt32
, and you must go through an intermediate type as you've discovered.
In the end, two double conversions are needed for the arc4random_uniform
:
let bounds = CGRectMake(0.0, 0.0, 500.0, 500.0)
var x = CGFloat(UInt(arc4random_uniform(UInt32(UInt(bounds.size.width) * 5))))
x -= bounds.size.width * 2
let center = CGPointMake(x, -bounds.size.height)
Alert that can work on iOS 7 and iOS 8
Explicitly add UIKit in the "Link Binary With Libraries" section of your project's build phases.
You can test for the existence of UIAlertController like this:
if NSClassFromString("UIAlertController") != nil {
// Use it
} else {
// Fall back
}I wrote a wrapper that works on both iOS 7 and iOS 8. You can find it here. It takes a view controller followed by a bunch of optional arguments and any number of buttons:
showAlert(self, style: .ActionSheet, sourceView: cell, completion: {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
},
(.Default, "Send clipboard", {
if someCondition {
// completion must be specified because of a Swift bug (rdar://18041904)
showAlert(self, title: "Nothing to send", message: "The clipboard is empty.", completion: nil,
(.Cancel, "OK", nil)
)
}
}),
(.Cancel, "Cancel", nil)
)
Swift String in String
In Swift is it called rangeOfString
"hello".rangeOfString("ell")
returns a range
{Some "1..<4"}
Related Topics
iOS 12 Wkwebview Not Working with Redirects
How to Compare Two Dates (Nsdate) in Swift 3 and Get The Days Between Them
Swift Nwlistener Listen, Cancel, and Relisten Successfully
Member Operator '==' Must Have at Least One Argument of Type
How to Set First Responder for Nstextview in Swift
Advantage of Key-Value Coding in Swift 4
Arkit - Raycasting Using a World Ray Instead of a Screen Point
Convenience Failable Initializers Assign Self
Cannot Get Newpassword to Generate Strong Password and Autofill
Alamofire Post Request with JSON Encoding
Why Does Compactmap Return a Nil Result
Move Button When Keyboard Appears Swift
Shorthand for Wrapping a Swift Variable in an Optional
Pass Type to Generic Function and Compare
Why Is My Libraries Not Able to Expand on The Cocoapods and Shows as Objective-C Not Swift
Localize iOS 14 Dynamic Widget Configuration
Event Scrolling (Scrollviewdidscroll) Never Called - Swift 3