Tuesday, 3 June 2014

Swift and Objective-C in the Same Project

Swift and Objective-C in the Same Project

Swift’s compatibility with Objective-C lets you create a project that contains files written in either language. You can use this feature, called mix and match, to write apps that have a mixed-language codebase. Using mix and match, you can implement part of your app’s functionality using the latest Swift features and seamlessly incorporate it back into your existing Objective-C codebase.

Mix and Match Overview

Objective-C and Swift files can coexist in a single project, whether the project was originally an Objective-C or Swift project. You can simply add a file of the other language directly to an existing project. This natural workflow makes creating mixed-language app and framework targets as straightforward as creating an app or framework target written in a single language.
The process for working with mixed-language targets differs slightly depending on whether you’re writing an app or a framework. The general import model for working with both languages within the same target is depicted below and described in more detail in the following sections.
image: ../Art/DAG_2x.png

Importing Code from Within the Same App Target

If you’re writing a mixed-language app, you may need to access your Objective-C code from Swift and your Swift code from Objective-C. The process described in this section applies to non-framework targets.

Importing Objective-C into Swift

To import a set of Objective-C files in the same app target as your Swift code, you rely on an Objective-C bridging header to expose those files to Swift. Xcode offers to create this header file when you add a Swift file to an existing Objective-C app, or an Objective-C file to an existing Swift app.
image: ../Art/bridgingheader_2x.png
If you accept, Xcode creates the header file along with the file you were creating, and names it by your product module name followed by adding “-Bridging-Header.h”. For information on the product module name
You’ll need to edit this file to expose your Objective-C code to your Swift code.
To import Objective-C code into Swift from the same target
  1. In your Objective-C bridging header file, import every Objective-C header you want to expose to Swift. For example:
    Objective-C
    • #import "XYZCustomCell.h"
    • #import "XYZCustomView.h"
    • #import "XYZCustomViewController.h"
  2. Under Build Settings, make sure the Objective-C Bridging Header build setting under Swift Compiler - Code Generation has a path to the header. The path must be directly to the file itself, not the directory that it’s in.
    The path should be relative to your project, similar to the way your Info.plist path is specified in Build Settings. In most cases, you should not need to modify this setting.
Any public Objective-C headers listed in this bridging header file will be visible to Swift. The Objective-C functionality will be available in any Swift file within that target automatically, without any import statements. Use your custom Objective-C code with the same Swift syntax you use with system classes.
Swift
  • let myCell = XYZCustomCell()
  • myCell.subtitle = "A custom cell"

Importing Swift into Objective-C

When you import Swift code into Objective-C, you rely on an Xcode-generated header file to expose those files to Objective-C. This automatically-generated file is an Objective-C header that declares all of the Swift interfaces in your target. It can be thought of as an umbrella header for your Swift code. The name of this header is your product module name followed by adding “-Swift.h”. For information on the product module name
You don’t need to do anything special to create this file—you just need to import it to use its contents in your Objective-C code. Note that the Swift interfaces in the generated header include references to all of the Objective-C types used in them. If you use your own Objective-C types in your Swift code, make sure to import the Objective-C headers for those types prior to importing the Swift generated header into the Objective-C .m file you want to access the Swift code from.
To import Swift code into Objective-C from the same target
  • Import the Swift code from that target into any Objective-C .m file within that target using this syntax, and substituting the appropriate name:
    Objective-C
    • #import “ProductModuleName-Swift.h”
Any Swift files in your target will be visible in Objective-C .m files containing this import statement. For information on using Swift from Objective-C code


Import into Swift
Import into Objective-C
Swift code
No import statement
#import "ProductModuleName-Swift.h”
Objective-C code
No import statement; Objective-C bridging header required
#import "Header.h”

Importing Code from Within the Same Framework Target

If you’re writing a mixed-language framework, you may need to access your Objective-C code from Swift and your Swift code from Objective-C.

Importing Objective-C into Swift

To import a set of Objective-C files in the same framework target as your Swift code, you’ll need to import those files into the Objective-C umbrella header for the framework.
To import Objective-C code into Swift from the same framework
  1. Under Build Settings, in Packaging, make sure the Defines Module setting for that framework target is set to Yes.
  2. In your umbrella header file, import every Objective-C header you want to expose to Swift. For example:
    Objective-C
    • #import <XYZ/XYZCustomCell.h>
    • #import <XYZ/XYZCustomView.h>
    • #import <XYZ/XYZCustomViewController.h>
Swift will see every header you expose publicly in your umbrella header. The contents of the Objective-C files in that framework will be available in any Swift file within that framework target automatically, without any import statements. Use your custom Objective-C code with the same Swift syntax you use with system classes.
Swift
  • let myCell = XYZCustomCell()
  • myCell.subtitle = "A custom cell"

Importing Swift into Objective-C

To import a set of Swift files in the same framework target as your Objective-C code, you don’t need to import anything into the umbrella header for the framework. Instead, import the Xcode-generated header file for your Swift code into any Objective-C .m file you want to use that code from.
To import Swift code into Objective-C from the same framework
  1. Under Build Settings, in Packaging, make sure the Defines Module setting for that framework target is set to Yes.
  2. Import the Swift code from that framework target into any Objective-C .m file within that framework target using this syntax, and substituting the appropriate names:
    Objective-C
    • #import <ProductName/ProductModuleName-Swift.h>
Any Swift files in your framework target will be visible in Objective-C .m files containing this import statement. For information on using Swift from Objective-C code


Import into Swift
Import into Objective-C
Swift code
No import statement
#import <ProductName/ProductModuleName-Swift.h>
Objective-C code
No import statement; Objective-C umbrella header required
#import "Header.h”

Importing External Frameworks

You can import external frameworks that have a pure Objective-C codebase, a pure Swift codebase, or a mixed-language codebase. The process for importing an external framework is the same whether the framework is written in a single language or contains files from both languages. When you import an external framework, make sure the Defines Module build setting for the framework you’re importing is set to Yes.
You can import a framework into any Swift file within a different target using the following syntax:
Swift
  • import FrameworkName
You can import a framework into any Objective-C .m file within a different target using the following syntax:
Objective-C
  • @import FrameworkName;


Import into Swift
Import into Objective-C
Any language framework
import FrameworkName
@import FrameworkName;

Using Swift from Objective-C

Once you import your Swift code into Objective-C, use regular Objective-C syntax for working with Swift classes.
Objective-C
  • MySwiftClass *swiftObject = [[MySwiftClass alloc] init];
  • [swiftObject swiftMethod];
A Swift class or protocol must be marked with the @objc attribute to be accessible and usable in Objective-C. This attribute tells the compiler that this piece of Swift code can be accessed from Objective-C. If your Swift class is a descendant of an Objective-C class, the compiler automatically adds the @objc attribute for you. For more information
You’ll have access to anything within a class or protocol that’s marked with the @objc attribute as long as it’s compatible with Objective-C. This excludes Swift-only features such as those listed here:
  • Generics
  • Tuples
  • Enumerations defined in Swift
  • Structures defined in Swift
  • Top-level functions defined in Swift
  • Global variables defined in Swift
  • Typealiases defined in Swift
  • Swift-style variadics
  • Nested types
  • Curried functions
For example, a method that takes a generic type as an argument or returns a tuple will not be usable from Objective-C.
To avoid cyclical references, don’t import Swift into an Objective-C header file. Instead, you can forward declare a Swift class to use it in an Objective-C header. However, note that you cannot subclass a Swift class in Objective-C.
To reference a Swift class in an Objective-C header file
  • Forward declare the Swift class you’re using:
    Objective-C
    • // MyObjcClass.h
    • @class MySwiftClass;
    • @interface MyObjcClass : NSObject
    • - (MySwiftClass *)returnSwiftObject;
    • /* ... */
    • @end

Naming Your Product Module

The name of the Xcode-generated header for Swift code, and the name of the Objective-C bridging header that Xcode creates for you, are generated from your product module name. By default, your product module name is the same as your product name. However, if your product name has any nonalphanumeric characters, such as a period (.), they are replaced with an underscore (_) in your product module name. If the name begins with a number, the first number is replaced with an underscore.
You can also provide a custom name for the product module name, and Xcode will use this when naming the bridging and generated headers. To do this, change the Product Module Name build setting.

Troubleshooting Tips and Reminders

  • Treat your Swift and Objective-C files as the same collection of code, and watch out for naming collisions.
  • If you’re working with frameworks, make sure the Defines Module build setting under Packaging is set to Yes.
  • If you’re working with the Objective-C bridging header, make sure the Objective-C Bridging Header build setting under Swift Compiler - Code Generation has a path to the header that’s relative to your project. The path must be directly to the file itself, not just to the directory that it’s in.
  • Xcode uses your product module name—not your target name—when naming the Objective-C bridging header and the generated header for your Swift code. For information on product module naming
  • To be accessible and usable in Objective-C, a Swift class must be a descendant of an Objective-C class or it must be marked @objc.
  • When you bring Swift code into Objective-C, remember that Objective-C won’t be able to translate certain features that are specific to Swift. 
  • If you use your own Objective-C types in your Swift code, make sure to import the Objective-C headers for those types prior to importing the Swift generated header into the Objective-C .m file you want to access the Swift code from.

Sunday, 23 March 2014

Facebook's New Programming Language : HACK


It is called Hack but it has little to do with hacking. It is a new programming language designed by Facebook that lets programmers build complex websites and other software quickly and without many flaws.
"We can say with complete assurance that this has been as battle-tested as it can possibly be," Bryan O'Sullivan, the Facebook engineer behind the language, said in a statement.
Experts say Hack is a new version of PHP -- the language Facebook founder Mark Zuckerberg used when he started building Facebook.
Hack too runs on the Hip Hop Virtual Machine but it lets coders use both dynamic typing and static typing. For the next decade, Zuckerberg and his rapidly growing company continued to build their site with PHP.
But as a PHP site grows, you need far more computer servers to run the thing than you would with other languages and it can be difficult to manage all your code and keep it free of bugs.
Hack makes it easier to manage code and eliminate errors. It provides these benefits without slowing down the developer. Unlike other statically type languages, Hack can run without compiling.
"You edit a file and you reload a web page and you immediately get the feedback. You get both safety and speed," O'Sullivan added.

Saturday, 22 March 2014

Things to be considered while developing Mobile Application


Every business big or small cannot neglect the power of connecting with their customers. There are various ways by which you can keep your brand in focus in customer’s mind. One of them is by building an app. Not only does it help in brand building, it also helps you in giving a better and rich experience of your service / product.
There are various reasons for creating your app like you want to make it an “extension” of your business or you want to make it your main business. Whichever may be the reasons, there are some common mistakes made by the newbie in this field, which you might want to avoid.
1. Not understanding the cost involved
So, you want to develop an app? Good. But do you have clear idea about what are the various costs involved? Here are few: UI Design Cost (one time), App Development Cost (one time), Apple / Google Play Developer License Cost (recurring annually) , App Marketing Cost (recurring), App Maintenance / Upgrade Cost (recurring), etc. Failing to understand and estimate this would lead to costly mistakes later on. So, plan and budget these costs before you start anything.
2. Trying to Save money at wrong places
Never ever try to save money by shopping for cheap and inexperienced UI designer or programmer. All money that you spend on UI design / programming is your investment. If you try to save $1 in UI design / programming, you will end up spending $10 in marketing. So, try to get the best your money can buy for these two aspects of your app.
3. Neglecting the power of Social media
Make sure you have Social Media (i.e. Facebook, Twitter, etc.) integrated in your app. Their power to spread the word about your app is tremendous. Any money spend here is your marketing money saved.
4. Spreading too much without understanding the audience
It’s important to know which devices are being used by your target users. If 80% of them uses an iPhone (for example), then it makes little sense to also develop the app in Android – at least not initially when you are looking to recover your development cost.
5. Not clear idea about how to monetize the app
“Everyone in my industry is making an app, so I should also make an app.”
Wrong. If you do not have any clear idea about how the app is going to help your business make money, then you will regret your decision to create the app. Have clear idea about how you are going to utilize your app to make money. Will it be free app based on Ads or will it be paid app? Or free app with in-app purchase? Think about this before you start.
6. Switching developers frequently
I have seen this happen many times. If you try to keep on changing your developers, then you will end up with code which in the end will become too complicated for anyone to handle. So, whenever possible, try to avoid switching developers.
7. Neglecting on testing the app
If you think you can do away with only your developer testing the app, then you are wrong. In the end, it’s your app. Your developer will try to find and resolve most of the bugs of the app. But he won’t be to test it from user perspective, which probably you can.
8. Not realizing the value of marketing
Why would anyone see and download your app when there are almost a million other apps available? That’s when marketing comes into play. You shouldn’t expect your brilliant app to do well without any marketing. Your job does not end with creating that beautiful app. You need to market it as well or you will end up losing your investment.
9. Failing the dumb user test
When you create the app, try to give it to the dumbest person you know and watch how he/she uses it. Your app should be so easily and intuitive in order to succeed in the market. Tons of features in the app would be useless if the users do not know how to use them.
Tips: Adding starting instructions overlay on how to use the app, can be useful in educating the users on how to use the app. Short videos and help section are useful too.
10. Adding all features in one shot
So you are planning to have total 10 features in the app and you want to release all these features in your initial version. Think again. If you give everything you have in the first place, what will you have to offer later on? Because in today’s market, if you want your app to succeed, you need to keep on add new features in them to have your users interest in the app. So, have big features in the initial release and then keep on adding small features on weekly basis and updating the app on the Appstore. That way your app will keep on coming in the list of apps sorted by release date.