Swift and objective-c runtime programming

Chris Eidhof: Dynamic programming is a very overloaded term, even when we restrict it to Objective-C/Cocoa. Free software coding For some, it means that a program shows dynamic behavior at runtime. Learn to play online poker For others, the use of KVO/KVC. Learn how to play online poker Or late binding, subclassing, swizzling, runtime casting, dynamic typing, and so on. Learn online poker free Depending on the person you talk to, it’s probably an extended subset of one of these things.

In Cocoa, when people say “dynamic programming” it often means runtime programming.


How to learn online trading In Objective-C, using the runtime has been a very import foundation of most frameworks. Learn online trading free By using runtime programming, we can write programs that are much shorter, and remove a lot of boilerplate. Currency forex learn online trading Little code and little boilerplate are good things. Ontario learn online courses However, there are also dangers associated with it: when you rely on reflection and introspection features of a language, it can be hard to find bugs statically: you’ll only find out at runtime.

Compared to Cocoa, UIKit has already moved away from runtime programming. Free cooking apps for kids Bindings don’t exist, and KVO/KVC is much less important than it was in Cocoa. Free cooking apps for android Swift moves way further in that direction.

Drew Crawford: Dynamic features are things that change the behavior of a program at runtime. Free cooking apps for ipad Examples include things like duck-typing (casting unrelated types that implement similarly-named methods), creating new classes at runtime, method swizzling (changing functions at runtime), selectors (deciding what functions to call at runtime), etc.

Cocoa was designed around many of these features, and assumed their existence as a fundamental principle. Best way to learn mobile app development Selectors are a key part of Cocoa’s target-action pattern. Best way to learn ios app development Going between classes and strings is a key part of NSCoding. Fat free cooking appliances And over the years in hundreds of small places these dynamic patterns have embedded themselves deeply into the Cocoa fabric.

InfoQ: In what respect does Swift fall short on them? What is that Swift makes hard to accomplish that is more easily resolved in Objective-C?

Chris Eidhof: The reflection mechanism in Swift is still very immature, and some runtime features aren’t available at all. Free cooking apps for iphone For example, with Objective-C objects you can use key-value observing to observe properties of an object. Best cooking apps for kids In Swift, you can only use that with Objective-C object, but not with structs.

Many things seem harder in Swift if you’re trying to solve them in a runtime-oriented way. Best free cooking apps for android The hardest part about giving up these runtime features is learning to think different. Best electric cooking appliances Almost all problems can be solved with much less code in Swift, but we need different approaches. Best kitchen cooking appliances Instead of using runtime programming, we can use features like closures, generics and protocols to solve the same problems in a shorter, safer way. Best buy cooking appliances For example, in the Swift Talk episode on Networking, we show how we can use generics to safely provide a very dynamic interface to a webservice.

Drew Crawford: One obvious example is XCTest, the Xcode testing framework. Best new cooking appliances XCTest can discover new tests you write by searching at runtime for all functions that start with “test”. Free cooking game apps for iphone Without that feature, you have to maintain a list of all your tests by hand so that XCTest knows how to call them. Best free cooking apps for ipad In practice, it’s easy to forget to include a test in the list, and that test won’t run, and so you aren’t testing the things you think you are.

Method swizzling is frequently used (some would argue misused) to work around vendor bugs. Best cooking apps for ipad It’s pretty common to get stuck in some situation where a closed-source library has some problem, and by digging around in the runtime you can insert your own code to work around it. Best app programming language The catch is, it’s likely to break when the vendor changes something, so it’s a double-edged sword.

InfoQ: Do you envision a more dynamic Swift? Should some level of dynamism be added at the language or framework level possible ? Should Swift be dynamic at all?

Chris Eidhof: Dynamic dispatch and message passing is already possible, either by using classes or by using protocols. Free android app programming More runtime programming support can be very useful, but may come at the cost of safety and performance. Best mobile app programming language Personally, I have not missed runtime programming at all, and I have been writing Swift full-time ever since the language got released publicly.

Instead of relying on runtime programming, I use all the other features that allow for dynamic behavior: closures, higher-order functions, protocols, generics, and so on. Best healthy cooking recipes This way, I can have dynamic behavior that’s statically checked by the compiler. Best healthy cooking apps In our book Advanced Swift we show all of these techniques in practice.

In a recent blog post, I wrote about how we can replace runtime programming (which is what makes Objective-C dynamic) with functions (which is what makes Swift dynamic). Best healthy cooking books I reimplement the NSSortDescriptor API just by using functions.

Drew Crawford: There are a lot of dynamic features already. Cheap healthy cooking recipes If you are on iOS/macOS, you have the full Objective-C runtime available, and you can bridge your Swift code into the runtime and go to town. Learning techniques So you can do dynamic dispatch, selectors, and method swizzling in Swift today.

The problem is mostly what we’re going to do for platforms like Linux, which don’t have the Objective-C tradition. Free online typing lessons And increasingly we’re going to have programs that want to be portable across the major platforms, and so there’s pressure on developers to stick to only those things that are portable when writing new code. Free online typing games That problem is largely unsolved.

But I think we have to consider why Swift exists in the first place. Free online typing test Swift is not Objective-C with modern syntax. Learn cooking online That would be a far easier project to undertake (and has been undertaken at many points in the language’s history). Simple recipes for beginners to learn cooking You do not need a new language, a new runtime, a new compiler, a new Foundation, if that is all you are trying to do.

Instead, Swift is a conclusion: that the way Objective-C is going is not where we want to go. How to learn cooking That the next decade of incremental improvements at the next 10 WWDCs will not get us to the right language, and the only thing for it is to revisit some very fundamental ideas, rewrite a lot of code, and kill our darlings.

I don’t mean to suggest the enemy here is dynamism, exactly. Learn indian cooking But I do think we need to let go of the idea that since Objective-C does things a certain way, Swift should also. Learn cooking basics If you like your Objective-C, you can keep it! Swift needs to chart its own course. I want to learn coding It needs to be aware of the history, but it also needs to learn from it, and that will mean doing things its own way.

Chris Eidhof: I don’t think a whole lot of features are missing. Learn app development Yet, there are two I’d like to see. How to learn app development First of all, adding more support for reflection could be very helpful, but it’s can be a double-edged sword: it might also make it easier to write incorrect code.

Second, and more importantly, I am really looking forward to conditional conformance in protocols. Learn software testing This allows you to be way more expressive. Learn software development For example, an Array does not currently conform to Equatable. Learn software engineering The reason is: it can only be Equatable if all its elements are Equatable. Learn online courses At the moment, this is not expressible in the language, but adding conditional conformance is a very high priority of the core team.

Conditional conformance will also allow us to do datatype-generic programming, which is used in languages like Haskell. Learn online marketing Datatype-generic programming is a typesafe way of writing programs that operate on the structure of your data (similar to runtime programming).

Drew Crawford: Two of the major problems plaguing Objective-C are performance and safety. Learn online typing What is less obvious is that these problems are an immediate consequence of Objective-C’s dynamism.

In a highly dynamic language like Objective-C, the programmer has incredible power. Learn online trading You can open up core system libraries and place your own code inside of them. How to learn software engineering You can hook into the very fabric of method invocation itself, creating objects with an infinite number of methods, or create methods that wink in and out of existence over the lifecycle of an object. How to learn software development It is a staggering, dizzying level of power.

But with great power comes great responsibility. Learn writing The more power you have, the less power is left for the compiler. Learn writing skills And so the Objective-C compiler has blinders on. Learn online free Your code *looks* like a simple for-loop through an array. Learnonline gmit ie But how do we know you have not replaced NSArray with some object that has an infinite number of methods? How do we know the array is not procedurally generated and has an infinite number of elements? This stuff sounds totally crazy, but in Objective-C it is street-legal, and inevitably somebody actually does it. Free online typing course So the compiler can barely get through a line of your program before resigning itself to exactly what you wrote, exactly the way you wrote it. Free online typing tutorial And what you wrote is unlikely to be very fast or very safe.

In Swift, we rescind the programmer’s license for omnipotent power. Free online typing practice But what we get in the trade is a compiler with the blinders off. Free online typing programs It can see much further into your code, and often has a bird’s-eye view of a whole program. Best way to learn android app development This lets it pull off optimizations that are truly breathtaking in scope, and it can sound alarms about subtle bugs that involve interactions between components at great distance. Best cooking appliances This is the force that attracts people to Swift, once you get beyond things like syntax and brace style. How to learn android app development It’s what gives Swift its character.

The puzzle is how we’re going to support the things Objective-C developers want, while maintaining the speed and safety that Swift developers expect. How to learn ios app development The reality is we will never totally square the circle, but if we put our heads together we can get closer.

Chris Eidhof: Yes, almost all discussions happen on the mailing lists, and the swift-evolution repository on Github is a great resource. Best cooking apps for android If that is too overwhelming, there is a weekly digest called Swift Weekly Brief.

Drew Crawford: One example is `_typeByName`, which arrives in Swift 3. Best cooking apps for iphone It’s the answer to Objective-C’s `NSClassFromString`, allowing dynamic construction of classes.

The fact that the API starts with an underscore should fill you with the appropriate amount of fear that the design has not been entirely thought through and will likely change in the future.

InfoQ: Swift aims to address a broad scope, including server-side development, and system programming. Cooking to learn How important is overall for Swift to address those concerns? Do you think the Swift dev team is listening?

Chris Eidhof: It’s clear that the Swift team is listening very well. I want to learn cooking They are very active on all the Swift mailing lists, and go to great lengths to involve the community. Where to learn cooking Swift is a very ambitious language, and I think the team really found a sweet spot between ambition and pragmatism. Want to learn cooking However, listening does not mean adding every possible feature to the language. Learn cooking app I’m very impressed how the Swift team always takes a step back, and tries to understand the actual problem, rather than implementing “one more feature”.

Drew Crawford: Swift faces a lot of challenges right now beyond dynamism. Best way to learn cooking The syntax and names of core functions are fairly unstable. Learn cooking skills There’s no ABI compatibility. Learn vegetarian cooking Generics aren’t complete. Learn cooking for beginners These are large, burning buildings. Learn cooking durban The core team is very, very concerned about these problems – in my view, correctly.

When the proposals on dynamism come in, the response is never “no”. Learn healthy cooking The response is that it’s far too important a problem to try to solve it while distracted by these other things. Learn cooking online free I empathize with the Objective-C developer who is annoyed by having to suffer for another year or two without better solutions on dynamism, but I think they will be more annoyed if we keep renaming half the standard library every year, and we have to pick our battles.

One thing to keep in view is that nobody in the world maintains more Objective-C code than Apple. How to learn cooking online Core Data alone probably has more dynamic tricks up its sleeve than the rest of the ecosystem combined. Learn thai cooking So the idea that Apple is unaware of the problem seems misguided; they are a lot better informed about both the benefits and costs of dynamism than perhaps anyone.

But I also think we need to let some things go. Learn cooking indian food The Swift Way™ is to think first about static ways to approach problems that were solved dynamically in Objective-C, and then maybe introduce some dynamism when other approaches fail. Learn about cooking If that means certain programs will always be better represented in Objective-C, we need to make peace with that, just as it’s clear to many of us many programs are better represented in Swift. Best book to learn cooking These differences give the languages character and flavor, and we need to give each of these languages permission to become their best selves, and to find their own path.

What Swift’s path looks like is still quite open. How to learn cooking basics I am confident it contains much more dynamism than the language has today. How to learn cooking fast But that story remains to be written, and I think it is too early to make firm conclusions about how the book will end as the community is finishing the third chapter. Learn cooking techniques Conclusion

In this article, we addressed the concerns raised by several Objective-C programmers about the lack of dynamic features in Swift. Learn the basics of cooking Two developers deeply involved in the process of advancing the Swift language and its library ecosystem helped us understand the context of those issues and how Swift can eventually tackle them. What do children learn from cooking About the Interviewees

Drew Crawford is a software developer, writer, and consultant. Learn cooking indian He wrote the first statically-linked Swift/Linux program. Best cooking shows to learn When not working on Swift, he runs an Austin-based boutique development company, writing iOS applications and server software and licensing custom Swift technology to companies of all sizes. Where to learn cooking wildstar Author of the widely-read “Why Mobile Web Apps are Slow”, his articles have been translated into a dozen languages and are assigned reading in mobile development classes at universities around the globe.

Chris Eidhof is a Swift developer from Berlin. Cooking to learn curriculum He started objc.io, a number of conferences, is the author of Functional Swift and Advanced Swift, creator of Deckset and Scenery, and host of Swift Talk. Learn about cookies In his copious spare time, he likes to run.