How do I use Objective-C code from Github in my Swift program? - objective-c

Summary of problem
I'm building a calendar app in Swift, and I'd like to start with the UI in this repo: https://github.com/erichoracek/MSCollectionViewCalendarLayout
This repo is written in Objective-C.
However, I'm not sure how to build on top of this Github repo using Swift.
Example of the UI I'd like to use in my calendar:
What I've tried
I successfully installed this library with CocoaPods.
I ran $pod install.
I successfully ran import MSCollectionViewCalendarLayout.
I added a new CollectionViewController on my Storyboard, and assigned it to my custom CalendarViewController class
I tried (dumbly) with the code below in class CalendarViewController,but I get this error message:
"Cannot assign value of type 'MSCollectionViewCalendarLayout.Type' to type 'UICollectionViewLayout'"
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView.collectionViewLayout = MSCollectionViewCalendarLayout
}
I understand I probably have to use self.collectionView.register(cellClass:forCellWithReuseIdentifier:)...but how?
There's some documentation titled "Usage" in the Github, but unfortunately I'm still lost as to how to use it:
There's also an Example.xcworkspace included, written in Objective-C. I've gone through most of it. They include header (.h) and implementation (.m) files for each of the elements that they use.
There's a good answer to a tangential problem here: https://stackoverflow.com/a/45540130/3979651, but this doesn't directly solve my problem. I'd like to import this Cocoapods library, instead of writing on top of Objective-C files. But I'm also not entirely sure if this is the right / best way to do it.
Summary of questions
How do I link my CollectionViewController to MSCollectionViewCalendarLayout so that it has the same UI?
How do I use self.collectionView.register(cellClass:forCellWithReuseIdentifier:) in this case?
Do I have to write a new file for each element, like in the Example? (Event cell, Day Column Header, etc.)
Or, would it be easier to just copy and paste the Example element files (.h and .m) into my app? If so, how do I build on top of those files?
What I'd like to accomplish
I would like to import MSCollectionViewLayout like a library, attach it to my own CollectionViewControllers, and build my own functionality on top of this, all using Swift.
Thank you all in advance! Hopefully I can commit the correct answers here to the repo's README.md file.

You can better use one of the below repos which are purely written in Swift and has the same UI as of MSCollectionViewLayout
1) CalendarKit
2) Calendar
Note: I personally used these libraries and these are easy to
integrate with your existing application.

Related

adding imports to objc bridging headers

I have been having an issue with using objective-c files in my swift project. I have come to the realization that I had already imported an objective-c file. So instead of importing another file, I decided to update the previous imported obj-c file with one more #import "STPAUBECSDebitFormView.h". this is for using stripes services.
photo below,
sorry for the false highlight, its STPAUBECS that I am having with.
with importing this file, I am getting hit with the 'STPMultiFormTextField.h' file not found and failed to emit precompiled header. However, if I were to remove that
import "STPAUBECSDebitFormView.h" I am hit with the image below,
I am trying to understand how to use multiple languages inside of swift. would really appreciate it if someone could help me understand how to use stripes obj-c language inside of swift.

Changing the implementation of Objective C method in Swift subclass

I am using an external library in my project. It is integrated via CocoaPods. I have access to the source code. The library is working well, but I need some modifications at some places in order for it to serve my purposes. It is written in Objective C. My project is in Swift.
I need to change the implementation of one method in the library. The problem is it is in the .m file and uses a lot of stuff declared only in the .m file. For example:
-(NSString*)methodToChange
{
NSArray<NSNumber*>* data = [self someInternalMethod:1];
uint value = data[0].unsignedIntValue;
return [self anotherInternalMethod:value];
}
I tried subclassing it like this:
class MySubclass : MySuperclassWithMethodToChange {
override var methodToChange: String {
//trying to use someInternalMethod and anotherInternalMethod
//unsuccessfully because they are not visible
}
}
The internal methods are using and modifying properties from the .m file that are also not accessible. Is there any way to solve this?
I would suggest forking the original library repository and making the necessary changes in your fork. You can then have your Podfile point to that. If your changes could be useful to others, make them in a way that doesn't break existing functionality and contribute them back to the library by opening a pull request.
If the original library gets updated later, you will have to merge those changes from the so-called "upstream" repository into yours. This does not happen automatically, so you'll have full control (and responsibility) over that process. See https://help.github.com/en/articles/syncing-a-fork for how this would look like.

Can Swift code embed in existing Objective-C file - or must one add that code to a discrete Swift file?

I have an Objective-c file which used to work with the older XCode's
with the emergence of XCode 6.x - code no longer performs
I found the same functionality in code written in Swift online
I read Apple's documentation about using bridging header's to allow the use of Swift with Objective-C. I was able to get that setup ok, I do believe.
What is not clear is - can I simply cut and paste the Swift code into my Objective-C file (a viewcontroller.me file actually), or am I limited to having to use a Swift file from the start ?
If I have to use a new file in my (very simple) app, Im not quite sure how to pull off having two viewcontroller.m files do the work where one used to work fine, so this method obviously creates a new problem for me.
Thanks for your assistance in advance !
I would recommend just creating a class or struct for your swift code, and access that class or struct in your Objective-C ViewController.

FMDatabaseAdditions methods not accessible in swift?

I am porting some existing code over to swift. I am using FMDB in swift and everything has been good so far, but I do not have access to FMDatabaseAdditions methods in my project in spite of having access to everything else (FMResultSet, FMDatabasePool and so on).
I have the bridging-header file with the #import "FMDB" statement and it has the includes for all the other FMDB files.
I have looked in my build phases and I can see FMDatabaseAdditions.m in the "Compile Sources" and I have moved it to the top of the list.
Still xCode doesn't recognise it or its methods.
Any ideas?
UPDATE: Head over to the link below (in bold) for an unofficial FMDatabaseAdditionsVariadic.swift file as provided by Robert. Also keep an eye out on the repo for when the fix is officially added in.
Thanks again Rob!
-- ORIGINAL MESSAGE --
I found out what is going on from the guys on github over at FMDB repo.. (see original post here):
I notice that some of the FMDatabaseAdditions methods work fine. For example:
if db.tableExists("test") {
println("test table exists")
} else {
println("test table doesn't exist")
}
It looks like Swift is having issues with the variadic methods within the category. This is a known problem with Swift, but while I created the variadic rendition of executeQuery and executeUpdate, I didn't do so for the FMDatabaseAdditions methods. I will take care of this.
Thanks for bringing it to our attention.
Thanks robertmryan for your help.

(ObjC) Unable to add libraries-Help please

I'm following an objC book and I need to use readline() to obtain a string.
However, before I can even get to that, I need to add the library that contains it. Therefore, I go into Build Phases -> Link Binary With Libraries -> Press the + ->Search and Add addlibreadline.dylib
After I do this, I have an icon under my top-level item that has the name of this library. BUT when I go into my code in main.m, NOTHING is imported. There is no #import
WHY? I have tried different things for hours and am frustrated!
Am I not understanding something about importing and libraries?
Btw: I am using Version 6.1.1 (6A2008a) and Yosemite 10.10.1 (14B25)
Thank you,
Since you've got the libraries linked, try adding this:
#import <readline/readline.h>
Then you should be able to use readline().
http://forums.bignerdranch.com/viewtopic.php?f=148&t=7617