Cocoa Pods now supports Swift code.

Downloaded an open source project Gulps and was trying to run “pod install”.

Suddenly came across this error:

Screen Shot 2015-05-21 at 9.09.33 pm

On further researching and going through this blog, got to know that, Cocoapods now supports swift pods (i.e pods written using swift language). We just need to add support for swift framework in our PodFile by specifying the command “use_frameworks!” at the end.

A sample could be found here : Gulps PodFile

However the support for swift pods have been added in Cocoapod version 0.36 and above and thus “use_frameworks!” command is recognised only in these versions. Older versions of cocoapods would give the above error. So we need to update our cocoapod to atleast version 0.36.

You can check your pod version by typing the following command in terminal:
pod  —-version

If the version is below 0.36, then update your cocoapod tool by running the following command in terminal:
sudo gem install cocoapods —-pre

On checking you should see your version upgraded to 0.36 or above.

Now go ahead and freely add swift libraries to your pod file !!!


Swift – Reduce method

I was fiddling around with the swift reduce method and came across some findings of how it works.

The reduce method is actually part of a functional programming concept called list folding. The list folding has many variations such as reduce, fold, inject and so on. This generally depends on the functional language that we use.

The folding methods operate on Collections such as Arrays, Lists, Ranges. The main purpose of the folding methods are to combine elements in the collections and produce a result.  Examples of such operations could be sum all elements in a given array, multiply only odd elements in a given array of integers and so on. The most important feature of any fold operation is that they are immutable i.e they do not change the values of the collection on which they operate.

Over here we discuss about the swift variation of list folding reduce. A very common thing about every folding method is that they have an inbuilt accumulator. An accumulator is like a rough book or a white board which is used by the folding methods to store the intermediate value that is obtained by applying the combining operation.

For Eg: Suppose we want to add all the values of a list [1, 2, 4, 5, 6]
So the operation would be performed in such a way:

Operation      : +
List                  :       1   2   4   5   6
Accumulator : 0    1   3   7   12  18

An accumulator can be set an initial value or can have a default value. The reduce method allows us to set an initial value.

To explain the usage of reduce, I shall try to solve the above problem in 3 different versions. The first being a typical way by iterating, then replacing it with the reduce method and finally adding some syntactic sugar provided by swift. Please note that I am using XCode Playground to write the code.

The first way to solve this problem is:

let numbers = [1, 2, 4, 5, 6]
var sum = 0
for number in numbers {
sum += number

Let us look at a more functional way of solving this problem in swift and that is using the reduce method. In reduce method the inbuilt accumulator substitutes the work carried out by the sum variable used in above solution. So our new solution looks as follows:

var result = [1, 2,4,5,6].reduce(0, combine: {accumulator, value in
                                                               accumulator + value

Let us try to understand what we are doing over here:
1.  The first parameter of  reduce method is the initial value of the accumulator.
2. The second parameter combine: takes a closure of type (U, T) -> U i.e a closure that takes two parameters and returns a value. This returned value is stored as the new value of the accumulator. Here U is the type of accumulator and T is the type of element in the list. In our case the accumulator is of type Int and the value in the list is also of type Int, so the closure that we pass over here would be of type (Int, Int) -> Int
So when the operation inside closure is applied on each element the accumulator will contain the result of the previous operation and value will contain the current value in the list on which the operation is to be applied.
3. We do not specify the types of the closure parameters as they are inferred by swift.

Now, in swift, if the last parameter of a method is a closure, then we can separate it out as follows:

var result = [1, 2,4,5,6].reduce(0) {accumulator, value in
                                              accumulator + value

Swift allows us to make this operation more concise. We can actually replace the closure parameters with their positions i.e $0 and $1, where $0 points to accumulator parameter and $1 points to value parameter. So the solution now becomes:

var result = [1,2,4,5,6].reduce(0) {$0 + $1}

Now as generating a sum of all elements in a list is a very common operation, Swift provides us with a + operation. So this expression can be rewritten as:

var result = [1,2,4,5,6].reduce(0, +)

As we can see, the initial 6 lines of code had been reduced to mere two  lines.

Time Mis – management

Hi Friends,
Its been a looong time since i have posted anything. A lot of it is because of not managing time properly. However, i do find out some time everyday to learn something new, but not able to write about them. So come out with an idea. Will try to post the links that i felt good and put a small one liners about what they helped me understand…

So heres the first one
Ruby SOLID Principles

Networking in Android using HTTP.

Android supports networking for enabling multiple functionalities ranging from social networking to cloud computing.

Few types of android applications that make use of networking are :

  • SMS (Short Message Service).
    SMS is a communication service component that enables the exchange of  short text messages between mobile devices.

  • Internet resource – based applications.
    Internet resource based applications rely on web content such as HTML (HyperText  Markup Language), XML (eXtensible Markup Language) and JSON (Javascript Object Notation).

  • Social networking applications.
    Social networking applications, such as Twitter, Facebook etc.. are important ways / methods for people to connect to each other.  

Source : The Android Developer’s Cookbook

Windows Phone 7 – Screen Orientation

Today, let us try to understand a bit about windows phone 7 screen orientation.

So, First Question : What do we mean by Screen or Page Orientation ?

Screen or Page Orientation is the way in which a rectangular page is oriented (aligned or positioned) for normal viewing Wikipedia

Generally there are two main types of orientation, depending on how a device is held.

  • Potrait – when the device is held in standing / vertical position.
  • Landscape – when the device is held in lying / horizontal position.

and, the next question : What Screen / Page Orientation does Windows 7 support?

Windows Phone 7 supports 3 types of orientations

  • Potrait (vertical, with the screen above the hardware buttons)
  • Landscape Left (horizontal, with the screen to the left of the hardware buttons)
  • Landscape Right (horizontal, with the screen to the right of the hardware buttons)

The SupportedOrientations property

A Windows Phone 7 application page can support all 3 types of page / screen orientation.

The SupportedOrientations property of a page allows us to specify the kind of orientation that a page can support. This property is present in the .xaml file of every page.

The values that these property can take are :

  1. Potrait : Supports only potrait orientation.
  2. Landscape : Supports only Landscape orientation. In this case, both the Landscape orientations i.e Landscape Left and Landscape Right are supported. However, we cannot choose to support only one of the Landscape orientations. If our app works with one of the Landscape orientations, then it has to work for both of them.
  3. PotraitOrLandscape : Supports all three orientations.

When we set the SupportedOrientations property to PotraitOrLandscape, the page automatically rotates to proper orientation at the appropriate times (based on the angle the user holds their phone and wether a hardware keyboard is activated).

System provided components, such as status bar, application bar, message boxes and other notificiations, adjust automatically.

The Orientation property

The Orientation property present in .xaml file is for design time use only. It is used in design-time, by Expression blend and Visual Studio to display the page in desired orientation.

Setting the Orientation property does not have any effect at runtime.

What orientation should be used when?

If users never need to do any typing inside our app, then it is okay for the app to be potrait only.

For apps, that involve typing, supporting landscape orientations is practically mandatory. The reason for this is that some phones have a landscape-oriented keyboard, and typing on it would not be a a great experience if its orientation doesn’t match the screen’s orientation.

Supporting only landscape is ok for some games and novelty apps, but weird for anything else. However, if such an app involves typing (such as typing username for high score etc), those parts should support the potrait orientation for the sake of phones with potrait-oriented hardware keyboard.

Reference : 101 Windows Phone 7 Apps – Vol 1 (Chapter 3)

Windows Phone 7 – The Application bar and the Application bar menu

The Application bar is the 72-pixel-thick strip of buttons docked on the edge of the screen adjacent to the hardware buttons.

Each button on the application bar represents a common action relevant to the current content (i.e page of the application being displayed).

An application bar should have only the the most commonly used actions – even if there are only one or two such actions. The rest of the actions should be placed in the application bar menu. We should avoid adding unnecessary clutter on the application menu. In case if there are no such commonly used actions then consider having an empty application menu and push the rest in the application menu bar. Don’t try to fit in any rarely used actions into the application menu just because it is empty, its still ok to keep them in the application menu bar.
Eg: Just because there are no commonly used actions, it doesn’t makes sense to put “about” and “settings” in the application bar, it is better to keep them in the application bar menu.

This is how a typical application bar looks :

This is how a typical application bar menu looks :

Using an application bar in your application

Any page can be given an application bar. This is done by setting its ApplicationBar property to an instance of ApplicationBar object.

It can contain upto 4 buttons (which are of type ApplicationBarIconButton) children and it also has a seperate application bar menu (which is of type Menuitems collection) that can be filled with as many application bar menu items as you want (which are of type ApplicationBarMenuItems instances) .

Reference : 101 Windows Phone 7 Apps – Vol 1 (Chapter 2)

XAML – (Learning Development for Windows Mobile 7 – Silverlight)

Its still early days for Windows Mobile 7.  But being one of those species who used to develop applications for Windows Mobile 5.0 – 6.5, just got interested to know what was new in Windows Mobile 7.  This curiosity led me to a path of exploration, which is what i would be sharing in different posts related to this series.

One of the most important technologies that Windows Mobile 7 extensively uses for application development is Silverlight. The – Learning Development for Windows Mobile 7 – Silverlight series – mainly focuses on the Silverlight aspect of Windows Mobile 7.

This post won’t cover much of Silverlight. I will cover that in some other post.  In this would like to focus more on a language called as XAML.

XAML (eXtensible Application Markup Language) is a declarative language that enables you to create and initialize .Net objects in XML.

Everything that we can do in  XAML, can also be done using code.  However XAML removes the language dependency required for building a User Interface i.e any person who is not familiar with .Net languages can also build the UI required for applications.

The XAML format enables a user to easily visualize a hierarchy of elements while separating presentation from code (i.e display\creation of UI is seperated from the implementation details such as handling of  button clicks etc..).


Reference – Silverlight 4 in Action

(To be continued)