Thursday, May 12, 2016

Learning object-oriented programming with Swift 2

Object-oriented programming, also known as OOP, is a required skill in absolutely any modern software developer job. It makes a lot of sense because object-oriented programming allows you to maximize code reuse and minimize maintenance costs. However, learning object-oriented programming is challenging because it includes too many abstract concepts that require real-life examples to be easy to understand. In addition, object-oriented code that doesn’t follow best practices can easily become a maintenance nightmare.

Swift is a multi-paradigm programming language and one of its most important paradigms is OOP. If you want to create great applications and apps for Mac, iPhone, iPad, Apple TV, and Apple Watch, you need to master OOP in Swift. In addition, as Swift also grabs nice features found un functional programming languages, it is convenient to know how to mix OOP code with functional programing code.

My book will allow you to develop high-quality reusable object-oriented code in Swift 2.2. You will learn the object-oriented programming principles and how Swift implements them. You will learn how to capture objects from real-world elements and create object-oriented code that represents them. You will understand Swift’s approach towards object-oriented code. You will maximize code reuse and reduce maintenance costs. Your code will be easy to understand and it will work with representations of real-life elements.

My book is organized in the following eight chapters.

Chapter 1, Objects from the real-word to the Playground, in this chapter we will learn the principles of object-oriented paradigms. We will understand how real-world objects can become part of fundamental elements in the code. We will translate elements into the different components of the object-oriented paradigm supported in Swift: classes, protocols, properties, methods and instances.

Chapter 2, Structures, Classes and Instances, in this chapter, we will start generating blueprints to create objects. We will learn about an object’s life cycle and we will work with many examples to understand how object initializers and deinitializers work.

Chapter 3, Encapsulation of Data with Properties, in this chapter, we will start organizing data in the blueprints that generate objects. We will understand the different members of a class and how its different members are reflected in members of the instances generated from a class. We will learn the difference between mutable and immutable classes.

Chapter 4, Inheritance, Abstraction and Specialization, in this chapter, we will start creating a hierarchy of blueprints that generate objects. We will take advantage of inheritance and many related features to specialize behavior.

Chapter 5, Contract Programming with Protocols, in this chapter, we will understand how Swift works with protocols in combination with classes. We will declare and combine multiple blueprints to generate a single instance. We will declare protocols with different types of requirements, and then we will create classes that conform to these protocols.

Chapter 6, Maximization of Code Reuse with Generic Code, in this chapter, we will learn how to maximize code reuse by writing code capable of working with objects of different types, that is, instances of classes that conform to specific protocols or whose class hierarchy includes specific superclasses. We will work with protocols and generics.

Chapter 7, Object-Oriented Programming and Functional Programming, in this chapter, we will learn how to refactor existing code to take full advantage of object-oriented code. We will prepare the code for future requirements, reduce maintenance cost, and maximize code reuse. We will also work with many functional programming features included in Swift, combined with object-oriented programming.

Chapter 8, Protection and Organization of Code, in this chapter, we will put together all the pieces of the object-oriented puzzle. We will take advantage of extensions to add features to types, classes and protocols to which we don’t have access to the source code. We will make sure that the code exposes only the things that it has to expose and we will learn how everything we learned about object-oriented programming is useful in any kind of apps we might create.

You can read more information about the book by clicking on the book's cover:


Intel® Modern Code: Understanding the need for modern code


Thursday, December 18, 2014

Work with UIKit UIImage in XCode 6.1 Swift Playground

XCode 6.1 was one of the finalists of the Dr. Dobb’s Jolt Awards 2015: Coding Tools. The Playground introduced in XCode 6 is extremely useful to easily test your Swift code snippets.

However, there is some confusion about the possibility of working with some UIKit classes and check the results in the Playground.

You can create instances of many UIKit classes in the Playground and check the results at the right-hand side of your code snippets. In this case, I’ll provide you an example of how to create UIImage, CGRectMake, and UIButton instances. Then, I will set a title for the button, change the title’s color and finally set a background image.

After you create a new Swift Playground that targets iOS in XCode, select View | Utilities | Show File Inspector. Check the value for Resource Path below Playground Settings. In my case, the Resource Path is /Users/gaston/Documents/GastonHillarPlayground.playground/Resources. Thus, if you want to load an image within the Playground, just copy it to the folder specified in Resource Path. In my case, I just want to load an up arrow image, and therefore, I copy Arrow-Phone.png to the /Users/gaston/Documents/GastonHillarPlayground.playground/Resources folder.

Swift Playground that targets iOS in XCode, select View | Utilities | Show File Inspector.

Then, you can enter the following code and use either the Quick View eye or the Circle with the Plus (+) sign to check the visual results.

import UIKit

let image = UIImage(named: "Arrow-Phone.png")

let rect = CGRectMake(0, 0, 300, 100)

let button = UIButton(frame: rect)

button.setTitle("Up", forState: UIControlState.Normal)

button.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)

button.setBackgroundImage(image, forState: UIControlState.Normal)

The following screenshot shows the results of clicking the Circle with the plus (+) sign for many code snippets and the visual results displayed at the right-hand side. Don’t forget to copy the images to the Resource Path folder. It’s the easiest way to make the Playground display images.
Work with UIKit UIImage in XCode 6.1 Swift Playground

Wednesday, December 17, 2014

How to check Intel AVX2 support on Mac OS X

If you wanted to check whether your Intel CPU included support for Intel AVX1 (short for Intel Advanced Vector eXtensions), also known as AVX1.0, you could execute the following command on a Terminal window:

sysctl -a | grep machdep.cpu.features

However, if you execute the command on a MacBook Pro running OS X Yosemite Version 10.10.1, with an Intel Core i5-4278U, you will notice only AVX1.0 is listed in the features list:

machdep.cpu.features: FPU VME DE PSE TSC MSR PAE MCE CX8 APIC SEP MTRR PGE MCA CMOV PAT PSE36 CLFSH DS ACPI MMX FXSR SSE SSE2 SS HTT TM PBE SSE3 PCLMULQDQ DTES64 MON DSCPL VMX EST TM2 SSSE3 FMA CX16 TPR PDCM SSE4.1 SSE4.2 x2APIC MOVBE POPCNT AES PCID XSAVE OSXSAVE SEGLIM64 TSCTMR AVX1.0 RDRAND F16C



You can check the official specs for an Intel Core i5-4278U CPU here, and you will notice the CPU provides AVX2 (indicated as AVX 2.0, and also known as Haswell New Instructions).

You have to run the following command on a Terminal window to check whether AVX2 is listed:


sysctl -a | grep machdep.cpu.leaf7_features

The results of executing this command in the configuration I mentioned before is the following:

machdep.cpu.leaf7_features: SMEP ERMS RDWRFSGS TSC_THREAD_OFFSET BMI1 AVX2 BMI2 INVPCID



As you can notice, the results include AVX2, and therefore, the configuration includes support for Intel AVX2 and you can use the necessary compiler options to generate code that takes advantage of this powerful instruction set.

Friday, December 5, 2014

High DPI in Windows 8.1: Checking the DPI Awareness with Process Explorer

If you invested in a high DPI laptop or a high DPI screen to enjoy crisp and clear text, you will be disappointed with some IDEs and other developer targeted applications that display both blurry text and images. As a developer, you spend a long number of hours reading documentation and hundreds of lines of code and your eyes will benefit from developer tools to be per-monitor DPI aware in Windows 8.1.

Unluckily, there are still too many developer tools that aren’t even system DPI aware and you cannot enjoy the benefits of your high DPI screen when working with them. If you see either blurry text or images in a window, you can easily check the DPI awareness of the related process with the Process Explorer utility. Notice the values of the DPI Awareness column in the following screenshot:


If you work with a multi-monitor workstation, you will have the best experience with the processes that indicate a Per-Monitor Aware value for the DPI Awareness column, as long as you have Windows 8.1 installed. So far, the latest version of Google Chrome (Version 39.0.2171.171m) is just system DPI aware (you will see the System Aware value in the DPI Awareness column for chrome.exe). Thus, if you drag one chrome window from one screen to another one that has a different high DPI resolution, you will not have the most possible crisp text and images in one of the screens.


If you are interested in diving deeper on high DPI displays in Windows 8.1 and how to develop apps that deliver crisp text and pictures on all the screens in which your application can be dragged to, you can read the two articles I published on Dr. Dobb’s that include many code samples and a complete explanation of the different modes in Windows 8 and Windows 8.1.

Monday, November 17, 2014

Discussing the Importance of SDKs & Software Tools at Intel Developer Forum 2014

A nice talk with Kathy Farrel at IDF 2014 (Intel Developer Forum 2014 at the Moscone Center, San Francisco, CA, USA) about the importance of SDKs & Software Tools provided by Intel that simplify making things. When you work with SDKs, you can focus on your ideas instead of trying to build your own stacks.

Getting started with the basic operations of the MongoDB C# driver

If you want to make the move from relational databases to the NoSQL world of MongoDB, these three articles I published on Dr. Dobb's might make it easy for you to use a C# driver to interact with a MongoDB database. Part 1: Getting Started with MongoDB Part 2: MongoDB with C# Part 3: MongoDB with C#: Deep Dive

Scala tutorial for C# Developers

You probably heard about the Scala programming language. If you spend most of your time with Visual Studio but you want to lean the popular JVM language, you can read my three articles published on Dr. Dobb's. In fact, C# 6.0 will include some features that are popular in the Scala programming language. Scala for C# Developers: A Tutorial Scala for C# Developers: Useful Features Scala for C# Developers: The Magic

Articles published on Dr. Dobb's

You can read all the articles about software development, programming languages, Big Data, High Performance computing and Internet of Things published on Dr. Dobb's here