Wednesday, 16 September 2015

Initializers - Swift

Initializers

In development, initializers prepare language elements for use. They establish default values for stored properties and perform basic setup tasks. Swift enables you to initialize instances of classes, structures, and enumerations, but it is the initializers in classes that prove the trickiest to work with. That's because classes adopt inheritance, enabling subclasses to inherit the features and initializers of their superclass.
A normal Swift instance initializer follows three basic steps:
  1. Initialize any instance variables created by your class to a default starting state.
  2. Call the superclass (if one exists) to initialize its instance variables. Yes, in Swift, you perform this action after you set up locally declared variables. This ordering ensures the class storage is consistent at all hierarchical levels.
  3. Perform any other setup duties (if any) required by your instance. Here is where you can override any inherited properties and call instance methods, and freely refer toself as a value.
The MyWindowControllerSubclass class in Figure 1 has superficially followed this standard pattern, adding a custom init() method in addition to the required init(coder:). This method sets up its new local variable and then calls super.init(). Despite this pattern, Swift won't build an instance using the parent's convenience initializer, the way it would in the Objective C code this example mirrors.


Figure 1 Convenience initializers may not be immediately available for use by subclasses. The required modifier on line 19 indicates an initializer that must be implemented wherever uninitialized storage is added in subclasses.
You might think, "Let me just create an initializer that redirects to the parent." As you see in Figure 2, that approach won't work either.


Figure 2 Designated initializers cannot call convenience initializers. They must call designated initializers in the superclass.
You must proceed carefully when working with Swift class initializers. If Objective-C is a big, warm, fuzzy teddy bear when it comes to initializers, Swift is a bespectacled, whip-carrying, leather-clad librarian with an attitude. Unless you follow certain rules, you find yourself fighting against the patterns that ensure your objects are initialized safely and consistently.

Designated and Convenience Initializers

Swift (and Objective-C, and many modern languages for that matter) uses two distinct initializer patterns. A designated initializer follows the three steps you just read about. It exhaustively establishes default values for all storage introduced by the class declaration. 
class RootClass {
var a : Int // local storage
init(a : Int) {
self.a = a // initialization
}
}
When you introduce a subclass, the subclass's designated initializer sets its new properties first and then calls an initializer that walks up the superclass chain.
class ChildClass : RootClass {
var b : Int // new subclass storage
init(a : Int, b : Int) {
super.init(a:a) // redirect to superclass
self.b = b // initialization }
}
convenience initializer provides a secondary construction utility. Although designated initializers should be few and functionally complete, a convenience initializer enables you to piggyback on designated initializers. They provide constructors that are shorter to call or provide an indirect initialization mechanism.
class ChildClass : RootClass {
var b : Int
self.b = b
init(a : Int, b : Int) {
// This convenience method requires only one parameter
super.init(a:a) }
self.init(a:a, b:0)
override convenience init(a: Int) { }
}
Convenience initializers are, as their name suggests, convenient or handy. For example, you might pass a string that holds the path to a nib for a view class as in the examples in Figures 1 and 2. Or you might provide an offset relative to the current time (timeIntervalSinceNow) in place of the canonical reference time (timeIntervalSinceReferenceDate) that anNSDate normally uses to set itself up.
Convenience initializers provide entry points built around the typical API needs of a client rather than the internal structure of the class. They offer a developer-friendly way to create shortcut patterns for instance construction.

The Rules of Initializers

Swift's documentation officially defines three initializer rules:
  1. Designated initializers in subclasses must call designated initializers in superclasses. This is the rule broken in Figure 2, which attempts to call a convenience initializer from a designated one. Designated initializers always delegate upward to other designated initializers and never to convenience ones.
  2. Convenience initializers must call other initializers (designated or convenience) defined in the same class. Convenience initializers always delegate sideways and may not walk up the chain to a superclass.
  3. Convenience initializers must end up redirecting to a designated initializer in the same class. Eventually the initializer chain from Rule #2 must end, and it must do so by calling a designated initializer declared in the sameclass as itself.
  4. No matter how much you keep delegating sideways, eventually you end up at a designated initializer that walks up the chain. And because designated initializers cannot call convenience initializers, that chain stops wandering as soon as you hit the designated item. By Rule #1, you walk up the class tree in a straight path of designated initializers.
    There's one more rule from the Automatic Initializer Inheritance section that goes like this:
  5. Any class that inherits or overrides its superclass's designated initializers may inherit its convenience initializers. Classes inherit designated initializers if they don't add new variables that need initialization. This applies to classes that don't define additional stored properties and classes whose new properties use default values defined outside initializers, for example var x : Int = 5.

Gopinath TB, 
CEO, Meteora Gaming
www.meteoragaming.com



Sunday, 22 February 2015

Tech NEWS { 23-Feb-2015 }

1. Apple hiring brightest brains to make car battery
            A year and a half ago, Apple had applied for just eight patents related to auto batteries. Recently, it has hired a bevy of engineers, just one of whom had already filed for 17 in his former career, according to a Thomson Reuters analysis.

2. US urges Lenovo customers to remove Superfish
            The US government has advised Lenovo Group customers to remove 'Superfish,' a program pre-installed on some Lenovo laptops, saying it makes users vulnerable to cyberattacks.

3. Google using new CAPTCHA to collect data for ads
            The 'CAPTCHA' has infuriated web users for years: It's that login test that asks you to type in a hard-to-read sequence of letters or numbers in order to prove are not a robot. Get one letter wrong and you'll be denied access.

4. Motorola to make ‘exciting’ announcement on February 25           
            Motorola has made a mysterious announcement about a product it is going to launch next week. The Lenovo-owned manufacturer has sent out invites to only international media, informing them of an event on February 25.

5. 1,500 mobile phones to be launched in 2015
            As mobile phone brands like Xiaomi, Asus, Motorola, Obi etc slug it out to increase their market share in India, around 1,400-1,500 new models are expected to be launched in 2015.

6. Cisco dumps bell curve assessment for appraisals
Cisco, for long one of the foremost practitioners of the bell curve methodology to evaluate employees, has decided to junk the practice and replace it with a new feedback mechanism. The company believes this will foster teamwork and collaboration.

7. Xiaomi to sue websites for selling phones illegally
Xiaomi will be taking legal action against websites selling its phones illegally and infringing on the company's trademark in India where the Chinese handset maker has an ongoing patent dispute with Ericsson. 

8. Samsung gives first look of Galaxy S6 in teaser
            Samsung has released a teaser video for its Unpacked event in Barcelona on March 1, and it's given us a sneaky look at the Galaxy S6 in the process. 

9. Lawsuit over default Google apps in Android dismissed
            A federal judge has dismissed a lawsuit accusing Google Inc of harming smartphone buyers by forcing handset makers that use its Android operating system to make the search engine company's own applications the default option. 

10.  Flipkart uses Amazon package as dustbin
            An image posted by Reddit India has created a buzz on social media and led to a Twitter war between e-commerce giants Flipkart and Amazon.

Gopinath TB, 
CEO, Meteora Gaming
www.meteoragaming.com





Friday, 20 February 2015

Tech NWES { 21-Feb-2015 }

1. Apple, Japan Display in talks to build $1.7 billion plant
            Japan Display Inc is considering building a new plant to supply smartphone screens for Apple Inc, which may shoulder much of the 200 billion yen ($1.68 billion) investment, Japan's Nikkan Kogyo newspaper said on Friday.

2.  Amazon wants to sell you designer labels
For the first time, a menswear version of Fashion Week will come to New York this summer. Bulbs will flash as photographers hustle to document the best looks from the spectacle's dapper attendees. Designer labels such as Calvin Klein, Michael Kors, and Rag & Bone will showcase their most creative garments on radiant catwalks.

3. Microsoft-Nokia deal disregarded workers' needs: Labour Union
The Labour Progressive Federation (LPF) has said commercial interests have trampled over genuine needs of workers of Nokia India mobile phone factory near here.

4.  Making your home 'smart', the Indian way
            Most home automation platforms offer a trifecta of 'sensors-apps cloud' — a central hub connected to sensors equipped to detect movement or heat; apps that allow users to control these sensors; and access to remote systems, or the 'cloud,' to store and analyze data.

5. Samsung Galaxy S6 to have smaller battery than Galaxy S5
            The launch of Samsung Galaxy S6 is just around the corner, and the rumour mill keeps on revealing new details about the upcoming smartphone. 

6. Angry Birds maker to debut in Chinese market
            Finland's Rovio will start tailoring its Angry Birds mobile games to the Chinese market with help from Beijing Kunlun as it aims to step up business in the world's most populous country, it said on Thursday.

7. T-Mobile Q4 revenue beats estimates
T-Mobile US, the No.4 US wireless operator, posted quarterly profit and revenue that handily beat market estimates as aggressive discounts helped it add more than 2 million subscribers.

8. Lenovo to stop pre-installing controversial software
            China's Lenovo Group Ltd, the world's largest PC maker, said it will no longer pre-install software that cybersecurity experts said was malicious and made devices vulnerable to hacking. 

9. Motorola replies to Apple's jibe over design
            It's hard to argue taste with Apple design chief Jonathan Ive, but that's exactly what Motorola's president seems to be doing in his defense of why the company allows customers to design their own smartphones.

10. Russian competition watchdog opens case against Google   
            Russia's Anti-Monopoly Service has opened a case against Google following a request from Russia's biggest search site Yandex, Interfax news agency reported on Friday citing the regulator.


Gopinath TB, 
CEO, Meteora Gaming
www.meteoragaming.com










Thursday, 19 February 2015

Tech NEWS { 20-Feb-2015 }

1. Motorola set to launch its costliest phone in India
It looks like Motorola is all set to introduce Moto Maxx, its high-end smartphone in the Indian market. Flipkart, which is Motorola's exclusive retail partner, has started teasing the phone via its social network accounts.

2. YouTube paid music service goes into beta
            Google Inc's YouTube is set to launch its long-awaited paid monthly subscription service called YouTube Music Key in a few months, said Robert Kyncl, the online video service's head of content and business operations, at the Code/Media conference.

3. Samsung buys Apple Pay competitor LoopPay
            Samsung Electronics Co Ltd has bought US mobile wallet startup LoopPay, signaling its intention to launch a smartphone payments service to compete with a recent offering from rival Apple Inc. 

4. Pinterest eyes $11 billion valuation with new funding
Pinterest, a photo pin-up site, is in talks to raise $500 million in a round of funding that would more than double its valuation to $11 billion.

5. At $8.7 billion, TCS fastest-growing IT brand globally
            The brand value of the country's largest software exporter TCS grew to $8.7 billion in 2015, clipping at a growth of 271% from $2.3 billion in 2010, making it the fastest growing software brand in the world over the past five years.

6. OnePlus One 16GB coming to India soon
Chinese manufacturer OnePlus is bringing the 16GB variant of its One smartphone to the Indian market next week.

7. Microsoft offers Rs 7,000 discount on Lumia 830, 930
            Microsoft Devices has announced a cash back offer of Rs 7,000 on its premium devices - Lumia 830 and 930 as part of its Holi promotions. The cash back amount will be offered upfront to all consumers who walk in to a retail store starting February 20. 

8.  Apple removes sensors from smartwatch
            Apple reportedly had some sensors removed from its new smartwatch after discovering that arm hair was obstructing its functioning.

9. Videocon to launch 68Mbps 4G service in UP, Bihar
            Videocon Telecom has plans to invest Rs 1,200 crore over three years on high-speed 4G mobile services to be launched in UP and Bihar by December at rates close to 2G and 3G internet services. 

10. Lenovo accused of exposing own laptops to hacking
Lenovo Group, the world's largest PC maker, had pre-installed a virus-like software on laptops that makes the devices more vulnerable to hacking, cybersecurity experts said on Thursday. 




Gopinath TB, 
CEO, Meteora Gaming
www.meteoragaming.com