Swift – Functions and Closures ( Part 3 )

Functions

Methods in Swift is called as function and define by func keyword, after this keyword you should write the name of your function, parameters by their types and names in parentheses and lastly -> and the return type.

func function_name ( argument_name : argument_type , ... ) -> return type{
   //Your functions's code here..
}

 Functions can take a variable number of arguments and also return more than one value.

Lets write a function that calculates min, max, avarage and sum values of an array.Read More »

Swift – Control Flows ( Part2 )

if and switch for conditions..

for, for-in, while, repeat-while for making loops..

You can write your conditions without using parentheses, but you should use braces. Condition should return a boolean value, otherwise you will get an error as “not an implicit comparison to zero“.

var isConditionTrue = false 
if condition{ 
   isConditionTrue = true 
}

In Swift, you don’t have to specific the type of your iterators in for-in statements.Read More »

Swift – Hello World! (Variables & Simple Values) Part1

This post is about learning to define variables, constants and simple values and saying Hello World! in a Swift way.

Hello World!

It’a classic, printing “Hello World!” first in a new programming language. It’s so easy to print a text in Swift too.

print(Hello, world!)

let for constants, var for variables.

let is used for defining constants and var for variables.

let myPiNumber = 3.1415
var myCircleRadius = 5

Read More »

Swift Tutorial

I’ve just started to learn Swift programming language after 4 years Objective C development experience. I want to share my experience in Swift language and tell about concepts in a very brief, clean and simple way as much as I can.

Screen Shot 2016-01-23 at 14.20.05First I started to use a playground to learn by experiencing and looking exact results without compiling and running. Playgrounds are very effective to write a code without having / opening any Swift project.

Being a learner / a starter / a newbie again was a little hard. You have to put all your learnings in Objective C to another space in your mind and think as simple as you can. In my very fist swift code, I found myself in a search for “how to make category (extension) in swift?”

After learning how to define constants (let), variables (var) and how to call methods. I tried to make a image with using a color, because of I didn’t find the backgroundColor property of a UIButton, all I’ve found is backgroundImage so I think I should write a extension to UIImage for having a initialising method by color. Yes, I wrote by digging a little into codes and stack overflow. But it takes some time, so I decide to learn in very beginning of Swift language and lead the way for developers like me (know Objective C very deeply and need to start learning Swift in a very clean, easy and fast way.)

This way please!

Part 1 – Define variables, constants and simple object types – Swift Hello World!
Part 2 – Use if, switch, for, for-in, while, repeat-while statements – Control Flows
Part 3 – Functions & Closures – How to call and define them
Part 4 – Objects & Classes – get, set, willSet and didSet methods of Properties
Part 5 – Enumerations & Structures – enum & struct in Swift

…To be continued!…

The Future Of Business

“You can’t connect the dots looking forward; you can only connect them looking backwards. So you have to trust that the dots will somehow connect in your future”, as a great man said in his inspirational speech.* Before making any estimation about long-term future of corporate business in the light of today’s business and social trends, looking backwards and indicating the milestones that change and shape lives in today’s convoluted world are essential.

It all starts with one brilliant innovation. After Internetworking spread out from CERN Research Center, it has touched people’s lives and linked people each other, technology comes in wide use in daily life, and information-sharing levels are very high. It’s changed the ways of human thinking and acting people routines, companies, even governments.

Read More »

Top Three Challenges in Organizations

Stress is one of the top challenges faced by employees in organizations and we all face with this issue from time to time in our daily lives not only as an employee but also as individual. In competitive business environment, Companies can set unreachable goals in terms of time, sometimes. Top management pushes their managers reach goals faster or they can ask them to achieve higher goals in same period of time that the managers cannot commit. The managers reflect the pressure they faced, to their subordinates and they also have to set tight deadlines, even these deadlines are not realistic. Tight deadlines and too much workload cause stress. Working under tight deadlines is shown as a ability that employees should have. People show this situation on their resumes as ability, and also companies ask employees that they can work efficient under pressure.Read More »

How to add Core Spotlight to index your app content on iOS9? (ADVANCED)- iOS9 Feature (Part 4)

I’ve mentioned about “How to add Core Spotlight to index your app content on iOS9?” in this article. If you haven’t read it, I really recommend you do.

This article will be about how to add multiple search item in very little and efficient code writing. Search items have some features such as title, contentDescription, rating, unique identifier , thumbData which will show the icon of your special content or basically your application, and so on.

Basically there are two advanced concepts you should know about CSSearchableItems / CoreSpotlight.

First one is your user can navigate anywhere in your app, by understanding which search item is tapped by your user in Spotlight. You can be sure of your users will find anything they search, by navigating them true navigation point of application.

Apple serves very basic app delegate method :

– (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray *restorableObjects))restorationHandler;

Second one is that you can list your search items and their attributes in a very simple but handy property list (plist) file and creating a very simple singleton class you can call it SearchManager, will help you to separate your search methods from other app delegate methods.

Screen Shot 2016-01-18 at 21.36.06
SearchableItems.plist

Soo, let’s start!

In delegate of your application, you should call your SearchManager for initializing to index searchable items at spotlight.

APPDELEGATE.

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
   [[SearchManager sharedInstance] initializeSpotlightSearch];
}
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray *restorableObjects))restorationHandler{

   if([userActivity.activityType compare: @"com.apple.corespotlightitem"]== NSOrderedSame){
      [[SearchManager sharedInstance] navigateFromSpotlightSearchItemContinueUserActivity:userActivity];
   }
return YES;
}

SearchManager.

You can find a brief explanation for following SearchManager code for Spotlight search.

#import "SearchManager.h"

@implementation SearchManager

static SearchManager *sharedInstance = nil;

NSString *const UNIQUE_IDENTIFIER_KEY       = @"uniqueIdentifier";
NSString *const DOMAIN_IDENTIFIER_KEY       = @"domainIdentifier";
NSString *const TITLE_KEY                   = @"title";
NSString *const CONTENT_DESCRIPTION_KEY     = @"contentDescription";
NSString *const THUMBNAIL_DATA_KEY          = @"thumbnailData";
NSString *const NAVIGATION_URL_KEY          = @"navigationURL";
NSString *const KEYWORDS_KEY                = @"keywords";
NSString *const KEYWORD_KEY                 = @"keyword";

+ (SearchManager *)sharedInstance {
    if (sharedInstance == nil) {
        sharedInstance = [[super allocWithZone:NULL] init];
    }
    
    return sharedInstance;
}

-(void)initializeSpotlightSearch{
//You should check if application is running iOS9 + devices,
//Otherwise your application will crash on iOS7 or iOS8 devices. 
    if(IS_IOS9_OR_GREATER){

        NSMutableArray *searchableItemList = [self getCSSearchableItemList];
        
        CSSearchableIndex *defaultSearchableIndex = [CSSearchableIndex defaultSearchableIndex];
        [defaultSearchableIndex indexSearchableItems:searchableItemList completionHandler:^(NSError * _Nullable error) {
            if (error)
                DLog(@"print error: %@", error.description);
            else
                DLog(@"indexed successfully");
        }];
    }
    
}

+ (NSDictionary *)getPropertyFileBySource:(NSString *)source andRoot:(NSString *)root {
    
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *plistPath = [bundle pathForResource:source ofType:@"plist"];
    NSDictionary *serachableItemsDictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];
    return serachableItemsDictionary[root];
    
}

-(NSDictionary*) getSearchableItemsPlistDictionary{
    return [SearchManager getPropertyFileBySource:@"SearchableItems" andRoot:@"spotlightSearchItems"];
}
- (NSMutableArray*)getCSSearchableItemList{
    NSDictionary *searchableItemsClientDictionary = [self getSearchableItemsPlistDictionary];
    
    return [self getCSSearchableItemList:searchableItemsClientDictionary];
}

- (NSMutableArray*)getCSSearchableItemList:(NSDictionary *)searchableItemsClientDictionary {
    
    NSMutableArray *searchableItemList = [[NSMutableArray alloc]init];
    
    for (NSString *searchableItemDictKey in [searchableItemsClientDictionary allKeys]) {
        NSDictionary *searchableItemDict = [searchableItemsClientDictionary objectForKey:searchableItemDictKey];
        
        NSString *uniqueIdentifier=[searchableItemDict objectForKey:UNIQUE_IDENTIFIER_KEY];
        NSString *domainIdentifier=[searchableItemDict objectForKey:DOMAIN_IDENTIFIER_KEY];
        NSString *contentDescription=[searchableItemDict objectForKey:TITLE_KEY];
        NSString *displayName=[searchableItemDict objectForKey:CONTENT_DESCRIPTION_KEY];
        NSString *title=[searchableItemDict objectForKey:TITLE_KEY];
        NSString *thumbnailData=[searchableItemDict objectForKey:THUMBNAIL_DATA_KEY];
        NSDictionary *keywords = [searchableItemDict objectForKey:KEYWORDS_KEY];
        
        NSMutableArray *keywordsArray = [[NSMutableArray alloc]init];
        for (NSString *akeywordKey in [keywords allKeys]) {
            [keywordsArray addObject:keywords[akeywordKey]];
        }
                
        
        CSSearchableItemAttributeSet *searchableItemAttributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:uniqueIdentifier];
        searchableItemAttributeSet.contentDescription = contentDescription;
        searchableItemAttributeSet.title = title;
        searchableItemAttributeSet.displayName = displayName;
        searchableItemAttributeSet.keywords = keywordsArray;
        if(IsEmpty(thumbnailData)){
            UIImage *thumbnail = [UIImage imageNamed:@"AppIcon"];
            searchableItemAttributeSet.thumbnailData = UIImageJPEGRepresentation(thumbnail, 0.7);
        }
        
        CSSearchableItem *searchableItem = [[CSSearchableItem alloc] initWithUniqueIdentifier:uniqueIdentifier
                                                                             domainIdentifier:domainIdentifier
                                                                                 attributeSet:searchableItemAttributeSet];
        
        [searchableItemList addObject:searchableItem];
        
    }
    return searchableItemList;
}

-(NSString*) getNavigationURLForSearchableItemKey:(NSString*)searchableItemKey{
    NSDictionary *searchableItemsClientDictionary = [self getSearchableItemsPlistDictionary];
    NSDictionary *searchItem = searchableItemsClientDictionary[searchableItemKey];
    if(!IsEmpty(searchItem)){
        return (NSString*)searchItem[NAVIGATION_URL_KEY];
    }
    return @"";
}
-(void) navigateFromSpotlightSearchItemContinueUserActivity:(NSUserActivity *)userActivity{
    NSDictionary *searchUserInfo = userActivity.userInfo;
    NSString *activityIdentifier = searchUserInfo[CSSearchableItemActivityIdentifier];
  //  NSString *activityType = searchUserInfo[CSSearchableItemActionType];
    
    NSString *navigationURL = [self getNavigationURLForSearchableItemKey:activityIdentifier];
    if(!IsEmpty(navigationURL)){
        // To do - write your codes for navigation, here.
    }
}

If you don’t want to navigate the spesific point at your application. You can ignore these following methods.
-(void) navigateFromSpotlightSearchItemContunieUserActivity:(NSUserActivity*)userActivity;
-(NSString*)getNavigationURLForSearchableItemKey:(NSString*) searchableItemKey;

PS: If you find this article useful please use like button 🙂 and if you have a question or a comment, don’t hesitate to share it!

May the Force Touch be with you! 🙂