How to define a CGPatternDrawPatternCallback in swift - ios

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).

Related

Not able to use Kotlin Extension Function written in common in iOS as Swift Extension

I have a Kotlin Multiplatform project setup with Android & cocoapods for iOS.
I have a file Extensions.kt in commonMain/src with the following function:
fun String.isValidEmail(): Boolean {
return validate(ValidatorRegex.EMAIL)
}
I am able to access this function in Android as a String extension:
"abcd#gmail.com".isValidEmail()
But in iOS using Swift, I need to call it as a static method of another class:
ExtensionsKt.isValidEmail("abcd#gmail.com")
It should convert that commonMain/src method to a Swift extension of String instead of a class with a static method.
Am I missing any configuration?
You're doing everything right here. Unfortunately, this option is not available for now. Extensions conversion may be performed correctly for some classes, but Swift's String is not the one. This is a known inconvenience, and K/N team is working hard to make it better.

NSLayoutConstraint has no member 'Attribute'

I am trying to convert swift code in following
NSLayoutConstraint.Attribute.height
I am using SkeletonView which it throws me error while executing.
I was not able to find the new replacement syntax of that
Type 'NSLayoutConstraint' has no member 'Attribute'
It all depends on the version of Swift you are using.
NSLayoutConstraint.Attribute.height // Swift 4.2
NSLayoutAttribute.height // Swift 3.0+
NSLayoutAttributeHeight // Swift 2?
Your swift version might not applicable with that syntax which used in swift 4.2
I am sure that your current swift version is low than that.
So, replace that line of code with
NSlayoutAttribute.height which applicable for swift version 3+ but below 4.2

StringByAppendingPathComponent() and WriteToFile()

I'm trying to learn Swift 3 with translating a code from Swift 2. In swift 2, I see the code like this:
return fullPath.stringByAppendingPathComponent(name)
But, when I try in Swift 3, I've got similiar code, but like this:
return fullPath.strings(byAppendingPaths: [name])
The issue is, return type in the 1st code is String (and that's the output I need from the lesson I learn), but return type in 2nd code should be [String].
The other issue is, in Swift 2 the code should be:
imgData?.WriteToFile(fullPath, atomicaly:Bool)
But in Swift 3, I only can input code like this:
imgData.Write(to: URL , option: WritingOption) throws
But in some examples, there's .Write(toFile: , atomically:) but I can't find it in Xcode.
Am I translating incorrectly or using both Swift 2 and Swift 3 incorrectly?
Regarding the first part of your question, as dan stated in the comments you should be using fullPath.appendingPathComponent(name) instead.
Regarding your second question:
The main difference between writeToFile and write(to: is the fact that the first is for Strings and the seconds is for NSData.
Somewhat related:
According to the NSData Class Reference
In iOS 2.0+ you have:
write(to:atomically:)
and
write(toFile:atomically:)
Given:
Since at present only file:// URLs are supported, there is no
difference between this method and writeToFile:atomically:, except for
the type of the first argument.
None of this has changed in Swift 3 according to the Swift Changelog.

Using SpeechKit for iOS using Swift

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?

How to use the CoreAudio API in Swift

I am in the process of migrating my streaming audio engine to swift. i am finding it difficult to use the C Audio API in swift.
I have a problem with AudioFileStreamOpen api where it takes 2 C functions as a parameter. I don't know how to use this API is swift.
AudioFileStreamOpen(self as UnsafePointer<()>, propertyProc, packetProc, kAudioFileMP3Type, audioStreamId)
I have defined the callback method as below for this API. But i am getting the compilation error.
func propertyProc(inClientData: UnsafePointer<()>,inFileStreamId: AudioFileStreamID,inPropertyId: AudioFileStreamPropertyID,ioFlags: UnsafePointer<UInt32>) -> Void {
.....
}
func packetProc(inClientData: UnsafePointer<()>,inNumberOfBytes: UInt32,inNumberOfPackets: UInt32, ConstUnsafePointer<()>, inPacketDescriptions: UnsafePointer<AudioStreamPacketDescription>) -> Void {
.....
}
Any help is appreciated to correctly define this C API in swift
You can't (currently) use an API requiring a C callback pointer from pure Swift code. Calling Swift functions or methods using a C function pointer is not supported by the current beta 4 language implementation, according to replies in the Swift forum at devforums.apple.com
UPDATE: The above answer is obsolete as of Swift 2.0
One alternative is to put some small trampoline C callback functions in an Objective C file, which can interoperate with Swift, and have those C functions in turn call a block or closure, which can be in Swift code. Configure the C callbacks with your Swift closures, and then pass those C callbacks to the CoreAudio functions.
I don't know much about Audio API, however, you should replace UnsafePointer by a pointer to an Object. for example:
var clientData : AnyObject?
var listenerProc : AudioFileStream_PropertyListenerProc = AudioFileStream_PropertyListenerProc.convertFromNilLiteral()
var packetsProc : AudioFileStream_PacketsProc = AudioFileStream_PacketsProc.convertFromNilLiteral()
var audioFileTypyeId : AudioFileTypeID = 0
AudioFileStreamOpen(&clientData, listenerProc, packetsProc, audioFileTypyeId, &streamId)
the initialization code for listenerProc, packetsProc or other variables is just to by-pass the compiler error.
To your situation, try to replace 'self as UnsafePointer<>' by '&self'. However 'self' must be something that can be converted to compatible data type.
https://developer.apple.com/library/prerelease/ios/documentation/MusicAudio/Reference/AudioStreamReference/index.html#//apple_ref/c/func/AudioFileStreamOpen

Resources