But turns out it gives an error saying the object cannot be converted to type Self. I found this elegant solution posted by Martin R on StackOverflow and it uses Generics.
-
Alternative to instanceType in Swift
Tuesday, August 23, 2016
In case you have not noticed, there is no straight forward alternative to using 'instanceType' in Swift. You might have tried using "Self" in place of instanceType like shown in the below code snippet.
But turns out it gives an error saying the object cannot be converted to type Self. I found this elegant solution posted by Martin R on StackOverflow and it uses Generics.
Posted by Adithya at 10:08 AM | Labels: Apple, Generics, Swift, Swift-Language, SwiftLanguage | 0 comments | Email This BlogThis! Share to X Share to Facebook |
-
Using Swift Class in Objective C project
Sunday, August 7, 2016
Steps for using Swift Class in Objective C iOS project
- Drag and drop our Swift file to our project. Make sure to prefix you're Swift class with @ObjC as shown in image below.
- You will be asked if a "Bridging-header" file should be added to the project as shown below. Select "Create Bridging Header"
- Assuming a project name "MyProject", this will create a file named "MyProject-Bridging-Header.h". This file is important to achieve what we are looking for even though we will not be adding anything to this file.
- Open Build Settings and configure some of the below mentioned values
- Defines Module - YES
- Embedded Content Contains Swift - YES
- Install Objective-C Compatibility Header - YES
- Now that you are in Build Settings screen, just search for "Swift Compiler", notice an entry is created for "Objective-C Generated interface Header Name" which will be a *-Swift.h file.
- You will need to import this file in your Objective-C header file that where you would want the Swift class to be available. In my case it will be "MyProject-Swift.h".
- After importing, you're Swift class should be accessible from the Objective-C file, if it still isn't, then just try some below trouble shooting methods. Sometimes the Swift Interface file will not be generated.
- Do a Clean Build and Run your code
- Delete Derived Data and repeat previous step.
Posted by Adithya at 11:25 PM | Labels: Apple, iOS, Objective-C, Swift, Swift Class, Swift-Language, SwiftLanguage | 0 comments | Email This BlogThis! Share to X Share to Facebook |
-
Generic Stack Implementation In Swift
Thursday, July 10, 2014
I'm pretty excited about this new feature in Swift; Generics. This is something borrowed from C++. The `Swift Programming Language` book stated "Generic Code enables you to write flexible, reusable functions and types that can work with any type, subject to requirements you define". Basically it just means you can write functions that can work with multiple types of parameters, collection types that can perform operations on the data set without any concerns of the type of the data set.
Below is a Gist of Generic Stack Implementation in Swift, a Stack that can work with any data types.
In the above snippet, you can see, <T> is the additions, right next to class name, that makes it a generic type. You can make use Type T within the implementation just like any other type.
You can check the sample project here in Github.Posted by Adithya at 1:47 AM | Labels: Apple, Generic Type in Swift, Generics, iOS, Stack Implementation, Swift, SwiftLanguage | 0 comments | Email This BlogThis! Share to X Share to Facebook |


