Fun with Objective C

http://blog.lukhnos.org/post/17270947434/currying-objective-c-methods

384 total views, no views today

Posted in iOS

Objective-C has that named parameter

I was surprised at how people are surprised at the named parameter feature of Swift. Objective-C in fact has that feature long ago

Swift

When encapsulated in a class (or struct or enum), the first parameter name of a method is not included externally, while all following parameter names are included externally when the method is called

class Person {
    func performThing1(thing1 :String, thing2 :String) {
        println("thing1 \(thing1) thing2 \(thing2)")
    }

    func performThing3(thing3 :String, _ thing4 :String) {
        println("thing3 \(thing3) thing4 \(thing4)")
    }

}

let person = Person()
person.performThing1("one", thing2: "two")
person.performThing3("three", "four")

Objective C

Methods can also take parameters, also called arguments. A message with a single argument affixes a colon (:) to the selector name and puts the parameter right after the colon. This construct is called a keyword; a keyword ends with a colon, and a parameter follows the colon. A method that takes multiple parameters has multiple keywords, each followed by a colon.

@implementation FTGPerson

- (void)performThing1:(NSString *)thing1 thing2:(NSString *)thing2
{
    NSLog(@"thing1 %@ thing 2 %@", thing1, thing2);
}

// Note how the 2nd external parameter name is discarded
- (void)performThing3:(NSString *)thing3 :(NSString *)thing4
{
    NSLog(@"thing3 %@ thing 4 %@", thing3, thing4);
}

@end

FTGPerson *person = [FTGPerson new];
[person performThing1:@"one" thing2:@"two"];
[person performThing3:@"three" :@"four"];

Reference

  1. Method names in Objective-C
  2. For Those Who Have Never Used Objective-C
  3. The Many Faces of Swift Functions
  4. Why must the last part of an Objective-C method name take an argument (when there is more than one part)?
  5. Message

432 total views, no views today

Posted in iOS

CocoaPods gotcha

Hi, in this post, I ‘ll share things I learn about Cocoapods

What goes into your .gitignore if you’re using Cocoapods

This question raises debate on SO, but I would recommend only track the Podfile and Podfile.lock. The Pods folder, .xcworkspace should go into .gitignore

Read more about the pros and cons Not Cuckoo for CocoaPods

pod install vs pod update

pod install

Every time the pod install command is run and it downloads and install new pods, it writes the version that it has installed, for each pods, in the Podfile.lock file. This file is intended to keep track of the installed version of each pod and to lock those versions

pod update

When you run pod update SomePodName, CocoaPods will try to find an updated version of the pod SomePodName, without taking into account the version listed in Podfile.lock. It will update the pod to the latest version possible (as long as it matches the version restrictions in your Podfile).

Read more Detailed presentation of the commands, Cocoa​Pods on NSHipster

You can edit the source of the Pod

Yes, you can edit the source of the Pod, and your change will be overriden by the next pod update, unless you use :path. See this reply by @orta

Your .xcodeproj remains unchanged

Actually, it remains unchanged after the first pod install
As pointed out Cocoapods

CocoaPods will create a new Xcode project that creates static library targets for each dependency, and then links them all together into a libPods.a target. This static library becomes a dependency for your original application target. An xcworkspace file is created, and should be used from that point onward. This allows the original xcodeproj file to remain unchanged.

The sandbox is not in sync with the Podfile.lock

It’s because of the Manifest.lock

This is a copy of the Podfile.lock that gets created every time you run pod install. If you’ve ever seen the error The sandbox is not in sync with the Podfile.lock, it’s because this file is no longer the same as the Podfile.lock. Since the Pods directory is not always under version control, this is a way of making sure that developers update their pods before running, as otherwise the app would crash, or the build would fail in another, less visible, way.

Where are the Specs stored locally

The Specs repo is pulled into ./cocoapods/repos/master/Specs

References

  1. February 2014 Tech Talk: CocoaPods
  2. UIKonf 2014 — Orta Therox: CocoaPods
  3. MCE 2014: Orta Therox – CocoaPods and the road to 1.0

500 total views, 4 views today

Posted in iOS

iOS static table view cells with varying row height and Autolayout

In this post, I ‘ll show how to achieve varying row height with static tableview and autolayout

Make sure your static cell Autolayout is setup correctly

This step is the same as dynamic table view, just make sure you add constraints to the contentView, not the cell

Dynamic height

Since this is a static tableview, you don’t need that prototypeCell trick, you can just use the IBOutlet directly. This is what happens in my UITableViewDelegate, note how I reference descriptionLabel

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1 && indexPath.row > 0) {
        return 0;
    }

    if (indexPath.section == 0 && indexPath.row == 2) {
        CGSize size = [self.descriptionLabel systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
        return size.height;
    }

    return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}

Reference

  1. Using Auto Layout in UITableView for dynamic cell layouts & variable row heights
  2. Dynamic Table View Cell Height and Auto Layout
  3. Table View Cells With Varying Row Heights

604 total views, no views today

Posted in iOS

Xcode build gotcha

Reference

  1. xcodebuild man xcodebuild
  2. xctool
  3. xcpretty
  4. Useful Xcode Build Phases
  5. Command Line Building using Xcode
  6. Building from the Command Line with Xcode FAQ
  7. Building Objective-C static libraries with categories
  8. Command Line Compiling and Debugging
  9. Build an iPhone app from the command line

520 total views, no views today

Posted in iOS

LLDB gotcha

452 total views, no views today

Posted in iOS

How to make all operations run on a certain thread in iOS

When using pjsip, I had a need to ensure that all pjsip calls must happen on a certain thread, because pjsip uses Thread Local Storage to prevent multiple threads from calling pjsip

Today, I read AFNetworking source code and see that it has this feature. All the code is in AFURLConnectionOperation.m

Read more ›

512 total views, 8 views today

Posted in iOS

AFNetworking gotcha 2

Hi, in this AFNetworking gotcha series, I’ll share something I learn about AFNetworking and remind you of some properties you may overlook

 

Read more ›

628 total views, no views today

Posted in iOS

Split an implementation file into multiple files in iOS

If you have a big class and and you want to split the implementation into multiple smaller parts, there are several options

Composition

A big class of course has many responsibilities. It’s your job to identify them and introduce new classes that is responsible for just one thing. There’s the Decorator pattern that may fit this, but under the hood, it is just composition

Use the include trick

@rob introduces the #include trick Split Objective-C Code into multiple files

Use categories

Objective C has categories feature, and we can take this approach

Using category and class extension

Suppose I have the big class called FTGPerson and I want to split it. Here I use class extension to provide private header and category for functions. Here the template and hope you can get the idea

Class

File -> New -> File -> Cocoa Touch Class
FTGPerson.h

@interface FTGPerson : NSObject

@property (nonatomic, copy) NSString *publicProperty;

@end

FTGPerson.m

#import "FTGPerson.h"

@implementation FTGPerson

- (void)method
{
    self.publicProperty = @"property";
}

@end

Class Extension

File -> New -> File -> Objective-C File -> File: Private, File Type: Extension

FTGPerson_Private.h

#import "FTGPerson.h"

@interface FTGPerson ()

@property (nonatomic, copy) NSString *privateProperty1;
@property (nonatomic, copy) NSString *privateProperty2;

@end

Category1

File -> New -> File -> Objective-C File -> File: Category1, File Type: Category
FTGPerson+Category1.h

#import "FTGPerson.h"

@interface FTGPerson (Category1)

@end

FTGPerson+Category1.m

#import "FTGPerson+Category1.h"
#import "FTGPerson_Private.h"

@implementation FTGPerson (Category1)

- (void)method1
{
    self.privateProperty1 = @"property 1";
}

@end

Category 2

File -> New -> File -> Objective-C File -> File: Category2, File Type: Category
FTGPerson+Category1.h

#import "FTGPerson.h"

@interface FTGPerson (Category2)

@end

FTGPerson+Category2.m

#import "FTGPerson+Category2.h"
#import "FTGPerson_Private.h"

@implementation FTGPerson (Category2)

- (void)method2
{
    self.privateProperty2 = @"property 2";
}

@end

Reference

  1. Learn Objective-C, Objects (Part 1): Splitting Classes Into Multiple Files
  2. An Objective-C class extension header – for what?

676 total views, 16 views today

Posted in iOS Tagged with: , , , , ,

Pet Arena

Our first game

Pet Arena is going to be the most exciting game ever.
In the role of a pet master, you have to take care of your pet, train it and have it compete with your friends’ pets via Bluetooth or Wifi. Pet Arena will be a social and fun game.

Read more ›

624 total views, 4 views today

Posted in Mobile Labs Tagged with: , , ,