Introducing TwitterText Swift library
From start of September ‘20 I was working on connecting my Code Conf app’s backend to Twitter API. Soon I got to the point where I have to properly calculate tweet lengths where they can consists from ASCII characters but also can contain emoji’s. This led me to read deeper through Twitter API docs where I found that there is available library making those calculations on much bigger range of Unicode characters than I even planned to use.
This set of libraries were written in many languages (including Objective-C) but not in Swift. As Code Conf backend API is hosted on Linux I needed Swift Package Manager compatible library for it.
I got two options:
- Try to calculate tweet lengths for myself, and almost for sure reinvent broken wheel, or
- Learn to read and understand Objective-C code and try to transcode this library to Swift.
I have chosen the latter option and decided that I will at least get new knowledge which will eventually help me in future of my iOS developer journey. I reminded myself that I probably have a book from which I can learn Objective-C. I wasn’t wrong. I found that I own book by Paul Hudson “Objective-C for Swift Developers” which I have bought last year as a part of Paul’s Swift Power Pack.
I have spent 2 days spent on reading it and felt ready to test my knowledge in practice. I have cloned twitter-text Objective-C library, created Swift Package Project and started transcoding Objective-C code to Swift.
I was tweeting a lot about this (insane for me then) idea of learning Objective-C in 2 days ;) At that time Rizwan contacted me on Twitter with question if I need some help on this project. I wasn’t sure, but felt that some kind of consultancy will be blossom when I will be stuck in some parts. It took me to 2 weeks to make this code compile and pass test suite on both macOS and Linux.
During development we found that Objective-C library uses IDNSDK library written in C. We have written Swift wrapper to this library and now it’s possible to use it also as Swift Package Manager dependency.
I was able to create most of the code by myself but finishing this library would not possible with lots of consultancy, knowledge sharing and huge amount of fixes provided by Rizwan. Especially for those parts where conversion of Objective-C code to Swift required proper type casting.
Thank you Rizwan a lot for your time and help. At this point I would like to also say thank you to authors of Objective-C version of twitter-text library: Satoshi Nakagawa, David LaMacchia and Keh-Li Sheng. Without their work I would not have a base upon which I have made this Swift library.
This was great time to extend my knowledge in many ways. 3 weeks ago I didn’t even thought that I will be working with Objective-C code and C code was totally out of my mind.
I’m pleased to share Swift twitter-text library with you.
The Swift implementation of the twitter-text parsing library, which allows you to parse Tweets and calculate length, validity, parse @mentions, #hashtags, URLs, and more.
Both libraries are open source and licensed with MIT License.
You can get them on Github:
PS. now I can go back to work further on connecting Code Conf backend to Twitter API :)
- | Paweł Madej
Licensing: Content Code Apache 2.0