Friday, June 26, 2015

Swift ?? operator

The nil coalescing operator (a ?? b) unwraps an optional a if it contains a value, or returns a default value b if a is nil.

Monday, June 8, 2015

Wrapping Google Analytics in Swift


import Foundation
@objc enum MyEventCategory: Int {
    case Category1
    case Category2
}
@objc enum MyEventAction: Int {
   case Action1
   case Action2
   
}
@objc class MyAanalytics  {
    
    func getMyCategory(category: MyEventCategory) -> String {
        return String(stringInterpolationSegment: category)
        
    }
    func getMyAction(action: MyEventAction) -> String {
        return String(stringInterpolationSegment: action)
    }
    func trackEvent(category: MyEventCategory, action: MyEventAction) {
        var label: String, value: NSNumber?
        label = "Any label" // whatever label you want to give
        value = nil
        
        let tracker = GAI.sharedInstance().defaultTracker
        let trackDictionary = GAIDictionaryBuilder.createEventWithCategory(self.getMyCategory(category), action: self. getMyAction(action), label: label, value: value).build()
        tracker.send(trackDictionary as [NSObject : AnyObject])
    }

}

Conditional unwrapping in swift using where clause


checking the optional bool variable as well as that is that variable is true or not


if canPlay = self.dataSource?.functiion() where canPlay= true {

}

Wednesday, June 3, 2015

Removing conflicts during merging two branches on bitbucket

On terminal

 $git status


remove untracked files if exists

$rm -rf fileWithFullAddress


$git status 

Repeat above till get clean directory


$git checkout FinalDestinationBranchForMerging


$git pull // get update for local branc


Now merge the other branch with it

$git merge MergingBranch

It will show the files having conflicts. Files will get -- head tags 

open Editor, make the changes


After making changes

$ git add ChangedFileWithAddress
$ git status

If need to remove/reset some file from the project 

$ git rest FileToBeRemoved

$ git add -A
$ git status

$ git commit -m 'The message I got while merging the branches'

$ git push

Done

Monday, June 1, 2015

IOS: Delegate and Datasource Swift and objective-c interaction

Delegate will be defined in the class from where the event need to be triggered
For example:
if class name is MyClass in swift

delegate will be defined as

@objc protocol MyClassDelegate: NSObjectProtocol {
    func myClass(myclass: MyClass, myFunction parameter: Parameter)
    

}

if some data is required in the class, the data source is defined as
@objc protocol MyClassDataSource: class {
     func myClass(myClass: MyClass, myFunction parameter: Parameter) -> data?  // like UUImage
}

------- in class implementation

var delegate: MyClassDelegate?
var dataSource : MyClassDataSource?

self.delegate?.myClass(self, myFunction: whateverParameter)
self.dataSource?.myClass(self)


----- using in objective-c

#pragma mark - AudioNoteViewModelControllerDelegate
@interface ObjectivecClass (MyClassDelegate) <AudioNoteViewModelControllerDelegate>
@end
@implementation ObjectivecClass (MyClassDelegate)
-(void) myClass:(MyClass *) myclass myFunction:(Parameter *) parameter  {
}
@end

#pragma mark - AudioNoteViewModelControllerDataSource
@interface ObjectivecClass (MyClassDataSource) <AudioNoteViewModelControllerDataSource>
@end
@implementation ObjectivecClass (MyClassDataSource)
-(Data *) myClass:(MyClass *) myclass myFunction: (Parameter *) parameter {
}
@end

-- in ObjectivecClass  interface

@property (nonatomic, strongMyClass * myClass;


--- in ObjectivecClass implementation viewDidLoad

     _myClass = [MyClass new];
    _myClass.delegate = self;
    _ _myClass.dataSource = self;