i'm trying to integrate Swift Project in An Objective-C application, I added the header "project-Swift.h" and I added also #objc before Swift class,
I resolved problems of compatibility but these too I didn't find solution for them:
func postCallsChangedNotification() {
NSNotificationCenter.Default.post(name: type(of: self).CallsChangedNotification, object: self)
}
Error: Use of unresolved identifier 'type'
func startSpeakerboxCall(completion: ((_success: Bool) -> Void)?) {
// Simulate the call starting successfully
completion?(_success: true)
/*
Simulate the "started connecting" and "connected" states using artificial delays, since
the example app is not backed by a real network service
*/
DispatchQueue.main.asyncAfter(wallDeadline: DispatchWallTime.now() + 3) {
self.hasStartedConnecting = true
DispatchQueue.main.asyncAfter(wallDeadline: DispatchWallTime.now() + 1.5) {
self.hasConnected = true
}
}
}
Error: Use of unresolved identifier 'DispatchQueue'/ Use of unresolved identifier 'DispatchWallTime'
Could any one help to find why these errors appears however this code works correctly before integration?
It looks like you are using Use legacy Swift Language Version > Yes, So DispatchQueue is not available and you are compiling for Swift 2.3 not 3, you have 2 options :
Use swift 3.
Update your function to 2.3 syntax.
Surely you can use swift files in objective c project.
Just make sure you have imported proper ProjectName.swift.h file in objective c file.
#import "ProjectName-Swift.h"
Refer swift usage in objective c project
Im trying to create a CGPattern using callbacks however the following method signature is not of CGPatternDrawPatternCallback type and I can not determine what it should be
func patternDefinitionFunction(#info: UnsafeMutablePointer<Void>, c: CGContext?)
You can't do this in Swift 1.2 or earlier; you'd have to write this part of your code in Objective-C. To write a C function in Swift, you'll need to update to Swift 2.0 (available in the Xcode 7 beta currently).
What is equivalent of setByAddingObject: in Swift. I have an NSSet property from Objective-C in Swift. But since Apple released Xcode 6.3 I have an error in this place:
Example:
NSSet *set;
object.set = object.set.setByAddingObject("text/html")
It produces error:
'Set<NSObject>' does not have a member named 'setByAddingObject'
Any idea, how to fix this? insert method doesn't work also.
Swift 1.2 added its own Set type, which is bridged over to Swift from Objective-C in a similar fashion to NSArray/Array and NSString/String. Try object.set.insert("text/html").
I know I can find the version of Swift I'm running right now reverting to a Terminal and typing:
xcrun swift --version
Swift version 1.1 (swift-600.0.57.4)
Target: x86_64-apple-darwin13.4.0
Also, I've been reading about the Preprocessor Macros in Swift, but no luck finding a Swift version constant.
As Swift 1.2 approaches it will be nice to flag old code that only runs on Swift 1.1 (Xcode up to 6.2) or new code that needs Xcode 6.3 (Swift 1.2)
Note: I can also use system() to do something like:
system("xcrun swift --version | grep version > somefile.txt")
Then open somefile.txt, but rather prefer some simpler solution
You can use conditional compilation directives to test for the specific Swift version used to build your project:
#if swift(>=5.0)
print("Hello, Swift 5!")
#elseif swift(>=4.0)
print("Hello, Swift 4!")
#elseif swift(>=3.0)
print("Hello, Swift 3!")
#elseif swift(>=2.2)
print("Hello, Swift 2.2!")
#elseif swift(>=2.1)
print("Hello, Swift 2.1!")
#endif
Finally got a workaround to do this. I'm using the constants prefixed with __ you can observe in your Playground. This would have been easier with some level of reflection, but...
__IPHONE_OS_VERSION_MAX_ALLOWED is 80200, meaning __IPHONE_8_2 for Xcode 6.2 (Swift 1.1) but its value is 80300 (__IPHONE_8_3) in Xcode 6.3 (Swift 1.2)
func isSwift12() -> Bool {
return __IPHONE_OS_VERSION_MAX_ALLOWED == 80300
}
isSwift12()
So now in your library you can fail fast and tell your user Swift's version is not correct using this:
assert(isSwift12(), "Need Swift 12")
Swift will give you a nice:
assertion failed: Need Swift 12: file , line 20
UPDATE WWDC 2015 - Swift 2.0
As stated in Apple's Swift blog, in Swift 2.0 we have #available blocks to check for certain OS versions in our code. An example should be:
if #available(OSX 10.11, *) {
monochromeFilter!.setValue(CIColor(red: 0.5, green: 0.5, blue: 0.5), forKey:kCIInputColorKey)
} else {
// Fallback on earlier versions
}
Swift 3.1 extends the #available attribute to support specifying Swift version numbers in addition to its existing platform versions.
// Swift 3.1
#available(swift 3.1)
func intVersion(number: Double) -> Int? {
return Int(exactly: number)
}
#available(swift, introduced: 3.0, obsoleted: 3.1)
func intVersion(number: Double) -> Int {
return Int(number)
}
From your comment:
I want to check because different versions of Swift have different features
You should not check the version of your programming language in order to use some features or not. This approach is much better:
if (self.respondsToSelector(Selector("yourMethodSelector"))) {
self.yourMethodSelector(test, sender: self)
} else {
//error handling
}
Just check whether a method is available or not.
Open up a command line on your Mac computer
type swift -version, without the double dashes, which doesn't work any more for Xcode 11/Swift 5.2.2
Of course, you'll have to update to latest IDE, which always downloads the latest version of both Xcode, languages, compilers and tools... EVERYTHING is updated. If you do that, that solution works for sure.
For iOS :
var systemVersion = UIDevice.currentDevice().systemVersion;
For OSX :
var systemVersion = NSProcessInfo.processInfo().operatingSystemVersion;
K.
I've used Nuance SpeechKit for some months using Objective-C. Now I'm coding in Swift - except for the class where I instantiate the SKRecognizer and SpeechKit has stopped working.
When I try to initialise the SKRecognizer object I get the following error:NMSP_ERROR] check status Error: 696e6974 init -> line: 485 makeFullPathname
Is it possible that it's because I'm using swift in the rest of the project?