"objective-c" entries

Translating your Objective-C project to Swift

A step-by-step approach to using Swift with Objective-C.

transmision

If you’ve got an existing app written in Objective-C, migrating it into Swift is an excellent exercise for learning Swift, experimenting with Swift, and deciding whether to adopt Swift on a full-time basis. I’ve performed this migration with several real apps, so here are some tips culled from my actual experience.

Hybrid targets

You’re surely not going to translate all your code into Swift at once; you’re much more likely to translate one class at a time. As soon as you add a Swift file to your Objective-C app target, you’ve got a hybrid target: some classes are written in Swift, while other other classes are written in Objective-C. Thus, declarations in each language will need to be visible to the other language. Before you start, it’s a good idea to understand how this visibility works.

Recall that an Objective-C class declaration is conventionally spread over two files: a header file (.h) containing the @interface section, and a code file (.m) containing the @implementation section. If a .m file needs to know about a class, it imports the corresponding .h file.

Visibility of Swift and Objective-C to one another depends upon this convention: it works through .h files. There are two directions of visibility, and they must be considered separately.

Read more…

New Swift language shows Apple history

Still reflects conventions going back to original adoption of Objective-C

A closer look at SwiftLike many Apple programmers (and new programmers who are curious about iOS), I treated Apple’s Swift language as a breath of fresh air. I welcomed when Vandad Nahavandipoor updated his persistently popular iOS Programming Cookbook to cover Swift exclusively. But I soon realized that the LLVM compiler and iOS runtime have persistent attributes of their own that have not gone away when programmers adopt Swift. This post tries to alert new iOS programmers to the idiosyncrasies of the runtime that they still need to learn.

Read more…

Upward Mobility: Animating the Leaves of Fall (in Australia)

It doesn't take many lines of code to make a snazzy animated background in iOS

One of nicest features of iOS development is that, frequently, you can pull off visual effects that look amazing without having to write a lot of code. It may be about to start the spring season here, but Down Under it’s heading into fall, so to honor those folks, we’ll animate some falling leaves. We’ll use a simple piece of code that will animate objects drifting down the screen.

I say drifting, because this trick only works for objects that are going to maintain a constant velocity as they descend through the space. Thus, it works well for things like leaves, pieces of paper, or other light objects that the mind will immediately accept as having reached their terminal velocity. In my case, I developed the code to handle confetti streaming down a “You’ve won” screen. Heavier or more streamlined objects require a bit more work to animate, because you need to simulate the effects of gravity and acceleration, otherwise the animation doesn’t look right.

So, to begin, we need some image assets. For this example, I’m going to use 3 leaf images I grabbed from a public clip art site, and cleverly renamed to leaf1.png, leaf2.png, and leaf3.png. I created a simple single-view storyboard project, and added a label in the center of the view. Now, all that’s left is to plop in the code:

Read more…

Transcending UIAlertView on iOS 7

RYO small floating view containing any interface you like

Of all the new features and APIs that iOS 7 provides to developers, none, in my opinion, is as important from a user interface perspective as custom view controller transitions, the ability to insert your own animation when a view controller’s view takes over the screen. Thus:

  • When a tab bar controller’s child is selected, you can now animate the change.
  • When a view controller is pushed onto a navigation controller’s stack, you are no longer confined to the traditional “slide in from the right” animation.
  • When a presented view controller’s view appears or is dismissed, you are no longer confined to a choice of the four UIModalTransitionStyle animations.

In the third case — a presented view controller — iOS 7 introduces a further innovation: You can position the presented view wherever you like, including the possibility of only partially covering the original interface.

In other words, the presented view controller’s view can float on top of, and partially reveal, the original view controller’s view; the user sees the views of both view controllers, one in front of the other.

Read more…

Upward Mobility: Black Magic Method Swizzling

Great hacks, great responsibility

This week, we’re going to depart from the basics, and talk about a piece of Objective-C black magic that, when used properly, can be a powerful tool. However, used incorrectly, it can cause devastation on a biblical scale. Let’s talk Method Swizzling.

Image that you have an app just about ready to go into the store, and your UI designer suddenly decides that all the UIButtons should use a different font.  Unfortunately, you have hundreds of them scattered over dozens of XIB files. Sure, you could go through and change each one by hand, but what happens if he changes his mind again?

Wouldn’t it be nice if you could programmatically force all the UIButtons in your app to use a new font? Maybe we can do it with a category? If we override the initWithCoder and initWithFrame methods of UIButton, we can call super and then set the font explicitly, right? Unfortunately, no, because super isn’t available in an overriden category method. But there is a way we can do it, using swizzling.
Read more…

Mastering iOS development

Matt Neuburg has suffered through the difficult bits of iOS development so you don't have to.

Matt Neuburg is an O’Reilly author and long-time writer for tidBITS.

We sat down recently to talk about iOS development and how best to build solid apps … the secret is take the time to learn the basics.

Key points from the full video (below) interview include:

  • All of the real power in iOS development is in C. [Discussed at the 1:26 mark]
  • Don’t use the Cocoa Framework, let it use you. [Discussed at the 2:56 mark]
  • Even though you don’t have time, understanding the underlying foundation of the OS makes you better a developer. [Discussed at the 8:07 mark]
  • Take a deep breath … Apple is actually working on improving the dev experience. [Discussed at the 10:54 mark]

You can view the entire interview in the following video.

Read more…

Objective-C and Cocoa: The core of solid iOS apps

Jon Manning and Paris Buttfield-Addison share their insight on what's new with Objective-C and Cocoa

Jon Manning (@desplesda) and Paris Buttfield-Addison (@parisba) are co-founders of Secret Lab and authors of the forthcoming Learning Cocoa with Objective-C, 3rd Edition

Key points from the full video (below) interview include:

  • Embrace Objective-C’s verbosity [Discussed at the 0:30 mark]
  • Just getting started with Objective-C? Check out the WWDC videos and… [Discussed at the 1:45 mark]
  • Long awaited updates to Objective-C make a big impact [Discussed at the 2:27 mark]
  • When it comes time to submit your app to the App Store, think about it as Apple would [Discussed at the 3:47 mark]

You can view the entire interview in the following video.

Read more…