Swift-Weekly is gaining some stance on GitHub really. i didn’t think this hobby project of mine could get so many people excited about Swift. Check it out here.
Tag Archives: trick
Swift Weekly – Issue 06 – The Swift Runtime (Part 4) – Generics
Generics are pretty cool. They let us do complicated stuff that many programmers don’t want to deal with sometimes and want to stick with traditional means of achieving the same goals but using basic ideas in OOP. In this edition of Swift Weekly, I won’t teach you about generics eventhough you may just see the examples and learn generics anyways. What I will teach you however is how generics are compiled at the assembly level buy the Swift compiler.
I am going to use the release version of the code to make sure the output assembly is as optimized as possible so that the optimization level is set to -O
in the output when your Swift files are being compiled. Also my swift -version
shows this:
Swift version 1.1 (swift-600.0.54.20)
Target: x86_64-apple-darwin14.0.0
I am using the latest beta of Xcode, aka Version 6.2 (6C86e)
. Let’s get started.
Note: I am going to get rid of some of the assembly code that is not relevant to the main point of this week’s objective.
Swift Weekly – Issue 05 – The Builder Pattern and Fluent Interface
A few weeks ago I started checking out some Wikipedia articles about various s/e design patterns and came across theBuilder pattern which is a Creational GoF pattern. Then as you know, I cannot just read one article in one sitting. I have to click every link that the article leads to, so I stumbled upon the article about Fluent Interfaces and I could then see the possibilities.
Note: Fluent Interfaces have nothing to do with IB or a visual interface that is displayed on the screen at all. Fluent interfaces are the way that we can write our software to ensure they are… well… fluent. Read on to understand how this works.
I don’t think fluent interfaces are the same as the builder pattern. I don’t really think fluent interface is actually a pattern at all. I believe that fluent interfaces are a concept, and a kick ass one at that. I think mixing fluent interfaces and the builder pattern will allos us to build Swift classes that are amazingly simple to use, instead of the classic OOP designs that we see on pretty much every Apple class these days. I wish Apple could read this article and (ehem), just update their iOS SDK classes for instance to use fluent interfaces and the builder pattern.
If you want to write your Swift apps in the most kick ass way, continue reading. I think this article will help you a lot not only in learning more about Swift, but also writing some really crazy code that will make your life and those around you much easier.
Swift Weekly – Issue 03 – The Swift Runtime (Part 2) – Enumerations
This is the second article in the Swift Runtime series of the Swift Weekly. In this article, we will dig deeper into the Swift Runtime and how the compiler deals with producing code for enumerations. Saturday morning writings are always fun! Let’s get this show started.
If you are an Objective-C or Swift programmer and have not done any Assembly programming or are simply not concerned with the low-level details of this article, jump right into the Conclusion section at the end to get the juice of this article.
Swift: Convert Unmanaged to String
Edit 1 (20th October 2014): Apple has now fixed this issue. To convert an unmanaged object to managed, just use the takeUnretainedValue() or the takeRetainedValue() method on it, based on whether you want to take a retained or unretained value.
So let’s say you have an Unmanaged<AnyObject> value that you know internally contains a value of type CFStringRef and you want to convert this to a value of type String in Swift. This is how I have managed to do that:
func convertCfTypeToString(cfValue: Unmanaged!) -> String?{
/* Coded by Vandad Nahavandipoor */
let value = Unmanaged.fromOpaque(
cfValue.toOpaque()).takeUnretainedValue() as CFStringRef
if CFGetTypeID(value) == CFStringGetTypeID(){
return value as String
} else {
return nil
}
}
Shown in Xcode it looks like this:
Creating string enumerations in Objective-C (The ultimate solution)
A while ago I wrote on my blog about a solution to one of the most common questions asked by Objective-C programmers which is “How can I create string enumerations?”. Well, the solution that I’ve given has immediately become one of the top subjects that attracts developers to my blog, as I can see in my stats. I thought I should now take it to a whole other level and get rid of the limitations that I had presented in the old solution, and come up with a fresh perspective.
The following video is the result of my work on this subject. I hope you’ll enjoy watching it.
Getting the most out of your WiFi dongle data plan (The definitive guide)
I am currently facing a situation where I need to carry out my work while I am connected to a WiFi dongle. I have spent about £20 for 6GB of data that can be used for up to 3 months, whichever comes first. So I have now learnt a lot of tricks on how to minimize my data usage and save money. Here, I want to share with you some of these tricks.
Trick 1 – Disabled images in your browser
If you view the web without images, your browser does not have to load those images from the internet so you will save A LOT of data. Every browser is different so I cannot tell you how to do that on every browser. Here is how to disable the loading of images on Safari on OS X:
- Go to Safari Preferences
- Go to the Advance tab
- At the bottom of the Advance tab, find the “Show Develop menu in menu bar”
- Then close the preferences window
- Go to the newly-shown Develop menu and choose Disable Images
That’s it. Some websites will not look nice without images and that just shows you how some web developers have now become too reliant on using images! Their fault really.
Trick 2 – Use Git commands carefully
I use Git through the terminal and two of the most useful commands that I use quite often are git fetch and git rebase. Now, git fetch, will fetch every new branch and all change-sets from the server and brings it into your local machine. What you want to do is to replace git fetch with the following command:
git fetch origin NAME OF BRANCH
So if you are on a branch named “ios7” and you want to just bring the remote changes on that branch to your local repo, do this:
git fetch origin ios7
git rebase origin/ios7
Now you got only the changes in that branch and you rebased to the latest changes
Trick 3 – Use Adblock… A LOT
Install Adblock on your browser and go to your popular websites and start blocking anything that looks useless and will consume your data. So just start blocking all content that is irrelevant.
Trick 4 – Disable Flash
I don’t have to explain this. Flash videos are memory, CPU and data hungry. They consume your battery so fast it is unbelievable. Just disable Flash to make sure your browser will not try to load those darn Flash videos for you.
Trick 5 – Stop watching videos on websites
Do not go to YouTube and other streaming video websites. They are very data hungry no matter how low you have set the quality of the videos. Watch videos when you are on a proper broadband, not on a tiny WiFi dongle.
Trick 6 – Subscribe to websites with push notifications
I read MacRumors quite a lot. I go to their website probably 3-4 times a day. But instead of doing that, it’s best that you subscribe to push notifications from news websites that offer that service. Then you can get push messages directly to your OS X desktop of new content that get posted on those websites instead of going there every time you want to check the news. A push notification will only contain the new article, not the whole website. But if you go and browse the website, you are viewing everything that is on the front page at least, hopefully without the images, as one of the tricks you learnt earlier!
Trick 7 – Stop opening a browser page ever time you think you’ve lost your internet connection
We all do this. When we think we have no internet connection, which can happen on a daily commute on a Wifi dongle, we open our browsers and navigate to our favorite website. If the website loads, we go “Aha, it works”. Instead of that, do this:
- Open Terminal
- Type this: ping http://www.apple.com
Once you see ICMP packets going and coming back, you know at least ICMP is working and you are almost 99.99% sure that your internet works.
Trick 8 – Do not preload top hits in your browser
Users of Safari or most modern browsers know that when they type in the search field for anything, a few “suggested” websites and terms will appear automatically. For instance, if you start typing “blood”, in your search field in your browser, you may get suggested words such as “blood pressure”, “check your blood sugar level”, and etc. Every time you type something in your browser’s search field, your browser will hit your search engine and look for suggested words and links. This will take battery and will use your internet. To disable this in Safari, follow these steps:
- Open Safari Preferences
- Go to the Privacy tab
- In the “Smart search field” area, click the “Prevent search engine from providing suggestions”
- In the same area, click the “Do not preload Top Hit in the background”
Okay that is it for now. These are all the tricks I have up my sleeves for saving you some data usage on your WiFi dongle. If you have any other tips that you believe are worth sharing with others, please let me know and I will include them here.
Enjoy
Debugging With LLDB and Xcode – Tutorial 1 – Populating Text Fields
In this video, I’ll demonstrate to you some of the baiscs of LLDB using the “expr” command to speed up your day to day development.