Tag Archives: program

Basic Swift for iOS: Core Syntax

Hi guys, I was given a Mac Book Pro recently and thought of learning some iOS development. So as a first step thought of going through the Basics first. And as you now sharing what I learn with other is the number one way of learning…! This blog will go in a format of short notes or points.
So here you go guys…!

Most of the basics are same as most other popular languages, but still there are some changes, Ill be high lighting the serous issues.

We don’t use semi colones at the end of the statements in Swift.
Also we avoid parenthesis when declaring a condition as much as possible, if there is a complex logic its advised you use it, other than that nope.

We follow the camel case naming convention in Swift

Playground

There is a tool called Play Ground in Xcode where you can write some code and observe the results soon as you write them. Its a great tool to learn the basics and to test some complex logics

Variables

In swift you don’t have to mention the type when you are creating a variable, it automatically decides itself according to the values you pass.

var myInt = 32
var myString = "cat"

But if you want to create a variable without assigning a value you can specify it like this

var myVariable :String
var myIntVariable :Int

Constants

Constants are variables with immutable values, so once a value is defined that value cannot be changed after that

So they are declared with the word “let”

let myConstVariable = 32
let myUnChangedVal = "car"

String Interpolation
Ooops don’t get scared by the word. Its basically a way of passing values or concatenating values in between strings.

var name = "Batzee"
var level = 11

just say you got 2 String values which u have to add in between a standard println() statement

println("\(name) is a Level \(level)" Agent in Ingress")

So the out put will be

"Batzee is a Level 11 Agent in Ingress"

So here instead of using ‘+’ operator we are using ‘\()’ operator

Functions

Functions in Swift are created with the key word ‘func’

func eatBurger(){
}

and called using the function name with the paranthesis

eatBurger()

If the function has a return type then the function is written as

func eatBurger() -> Int{
var burgerNo = 3
return burgerNo
}

‘->’ followed by the return type must follow the function before the curly braces starts.

if Statements

if statement is same as you have written an ‘if’ statement anywhere else, but there are certain things you have to consider when writing in Swift.
As always the Swift says it’s best practice if you can avoid the parenthesis around the condition
And on statements that has one line of executable line of code on success also must have it self surrounded by the curly braces unlike other languages, where you can avoid using the curly braces around the single line code.

if  x<500  {
return x
}

Loops

So there are 4 kinds of loops in Swift

The 1st one is the traditional for loop there is not much difference, only thing you have to consider is to remove the parenthesis around the condition

for  var x = 0; x<10 ; x++  {

}

The 2nd one is the for in loop this is something like foreach loop you specify a collection to loop through. Whatever you pass it converts it in to a collection. If you give a range, it loops through the range. If you pass an Array, it loops through the array element, and if you pass a string it converts the String in to character array and loops through the character array elements

Example 1

var total = 0

for x in 1...100{
total = total+x
println()
}

So the x will loop from 1 to 100

Example 2

var name  = "cat"

for y in name{
println(y)
}

here the variable name will be taken as a string array and the out put will be

c
a
t

The 3rd one is the While loop, it is same as any other while loops in other languages, only considerable change is, not necessarily to add the parenthesis around the condition

while condition {
// do if the condition is true
}

The 4th one is the Do While loop, in this there is some noticeable change, the condition is added at the end of the body, this is to run the code inside the body at least once before it checks on the condition

do{
//runs for the 1st time and runs for the second time and after, if the condition is satisfied
}while condition

Range

Range operator is something new in Swift

which is defined by ‘…’ this says to consider the number mentioned in the left of the operator and all the numbers in between it and the number mentioned in the right of the operator.
Also in the Range operator you can control the 1st and the last dot, like ‘..<‘ , ‘>..’ so this basically will work like a for loop

for  x in 1...100 {
println(x)
}

this will print 1 to 100

similarly

for y in 1..<100{
println(y)
}

will print 1 to 99

Switch Cases

So the Switch Statement in Swift has some points to consider .

The case range must be Exhaustive, unlike other languages you cant check only for a handful of conditions but for all the possible values.
for example if you are checking for integer cases, integer is never ending so u have to handle it. In that case you will use the default case to catch all the situations that you don’t want than what u actually wanted.

let chocoCount = 7
Switch chocoCount{

case 0:
//in case 0 do this
case 1:
// in case 1 do this
case 12:
//in case 12 do this
default:
// do this if the cases dont match the given cases

}

Also in Swift’s Switch statements they don’t have the ‘fall through’ option, which automatically falls into another case for a range of cases. In that case we can use the range case, We have already seen what is a range operator does above.
This is how it is done.

switch chocCount{

case 1...7:
//if the case is between 1 and 7 do this
case 8...15:
// if the case is between 8 and 15 do this
default:
break

}

This post is written after learning from a video, so there are some chances that I may have made some mistakes, so please feel free to comment them down, Ill me more happy to correct them. And stay tuned in for more blogs on Swift and iOS development.

Reference: Notes are taken by studying the Swift Essentials from Lynda.com

Lets try Android Game Development with AndEngine

Hi Guys, this time I am back with some Android Game Development. I got this idea after, the popular game Flappy Bird. Hope all may have tried that game…!
Ok, so I wanted to develop a game and I did some researches and found out that there are a lot of Game engines out there.  Some of them are COCOS2D, AndEngine, libGDX and many others. In this article I’ll guild you using AndEngine which is a bit easier one. In future I’ll write on libGDX.

Assumption : I assume that you guys know how to install eclipse development environment, configure java path and install Android plugin to eclipse. This post does not cover all those basic steps.

Advise : As this is a kind of a crash course, its advisable to create files the same name as I use(But it is not mandatory).

Expected Outcome : You will be able to create some lines using the AndEngine

Step 1
Download the Source(library) for the AndEngine using this link and extract
https://github.com/nicolasgramlich/AndEngine

Step 2
Create a new project and when creating select ‘Android Project from Existing Code’ and in it select the folder that we extracted in the Step1.
existing code

This is a library project. Actually this library is the ANDENGINE.
libraryproject

You can check it by right clicking on the project we created now and going to the Android tab. You can see the ‘Is Library’ check box ticked.

Step 3
Now create a new Android project and un tick the create activity. Because we are going to create AndEngine activity using the library project we created above.
createactivity

Step 4
Now you have a project with empty src folder. Right click on that folder and create a new package when naming the package use the package name available in that project ManifestFile
packagename

In my case package name is ‘”com.batz.andengsample”‘

Step 4
Now in side the empty package Download or copy the code and create LineEample.java activity using this LineExample Source Code.

You will get some errors, but don’t worry we will fix it in the next step.

Step 5
So now in your project, right click and go to Android tab and there in the below section(library section), click on the add button and select the AndEngine project and press ok. And then press Apply.
add library

Now all the errors may have gone. Some times you will have errors on the package name, if you have used your own name. So for that, click on the package name that shows as error, click ctrl key +1 key , which will bring suggestion to create a package name according to yours, and click on it to create it. And hopefully all the bugs will be fixed.

Step 6
Before going for the app launch, we have to do one more important thing, we have to specify the activity we created now in the manifest file as a launcher

So go to the manifest file and add these lines between the application tags

         <activity
android:name=”.LineExample”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
manifest

LineExample is the name I gave for the activity we created, so in your one don’t forget to change that

Step 7
Now you are ready to run the app. So while you are in the the activity try running the app. it should bring a screen where you will see lots of random lines.

Step 8(For people getting error saying cannot find andengine.apk)
For some people the AndEngine folder is not getting added as library on the time of building the app. So at that point what you can do is copy the andengine.jar file from the AndEngine project libs folder and copy it into your projects’s libs folder. And right click on the newly added andengine.jar file and click on ‘Add to Build Path’ option. This is how the project structure looks after I added the jar to my project from the AndEngine project in the Step8

projectstructure

AndEngine Resources : http://www.andengine.org/

For people who got more time , for people who needs more detailed instruction on the same project guild please watch this tutorial video
Development Reference : http://www.youtube.com/watch?v=Q0kjiIH6u-M

Thank You
Happy Coding…!

-Batz-

Google Glass Deep Inside(In Programers’ View )

Basically the Google Glass runs Android OS. It means if you know Android(or Java) you can easily write apps for the glass.

If you want a basic idea about what is google glass please read my previous post.

But there are some important stuffs you have to look into and research, before you jump into the app development. With limited hardware resources, the capability of the devices is restricted. At the time of writing this article the Explorer Edition 7 update is released and we can do some stuffs like, take a photo, record a video, start a hangout, send an email, browse web and some other stuffs.

The device is not like phone, where it does not have a touch display, the glass works by voice commands and mostly by tapping and scrolling the side of the glass. So you cannot expect to add buttons sliders. Still an SDK is not released to develop app for the glass.

If you have been wondering on what would it take for someone to get up and running with google glass development. Following is a list of technologies that forms the technology stack for google glass:

  • Web service protocol: REST
  • Message format: JSON
  • Google Mirror  API: Java, .NET, PHP, Python, javascript, Ruby

Well, we can start on with what Google Glassers have termed as “Glassware“. Glassware is a piece of software built on top of Google Mirror API. Google Mirror API is a set of Restful web services which help to communicate to and fro with google glass devices. Lets look at the diagram below to understand a little better.

GoogleGlass1

Above depicts interaction between google glass device and glasswares based on google mirror API.

What one can do using Google Mirror API?

  • Creating new cards on a timeline
  • Updating current cards on a timeline
  • Receiving user-initiated input exposed through a card’s menu
  • Subscribing to notifications from Glass devices to be notified of user actions

lets see the Google Glass – Mirror API – Glasswares – High Level Architecture

google-glass-high-level-architecture

Lets try and understand the above architecture:

  • Glass users subscription to your service: You develop one or more web-based service for google glass and publish. These services can be termed as glasswares. Glass users subscribes to your service by logging on to your website authenticating themselves using OAuth 2.0
glass camera
User subscription by visiting a glassware site
  • Glassware updates using Mirror API: Your google glassware then starts publishing to google glass cloud using mirror API. To be able to that, you must register your service with google cloud.
  • Google Glass synchronizes with glass device: This is third aspect. Once your glassware publishes to google cloud using mirror api, google glass then synchronizes your updates to your subscribed users.

You can control the glassware sites you have subscribed by logging in to MyGlass account . You can access this with the MyGlass Android App too.

final
User can On and Off Notifications to glass from this online site

In the above image you can see my glassware “Batman’s Layer” is listed with the other glasswares I have subscribed.

Ok then In my next post we will see what you needed to start developing for Google Glass …!

References: http://vitalflux.com/tag/google-glass-2/page/2/