Understanding new code base

This post contains some tips I found useful when navigating to a new code base. Some may overlap with each other, but you know the point.

Aspects

This is the best library for Aspect Oriented Programming on iOS. Furthermore, its hooking system is useful

For example, this allows you to know which ViewController you are in

[UIViewController aspect_hookSelector:@selector(viewWillAppear:) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> aspectInfo, BOOL animated) {
    NSLog(@"View Controller %@ will appear animated: %tu", aspectInfo.instance, animated);
} error:NULL];

FLEX

This allows us to easily detect which views we are seeing. I see that it’s much better than Xcode 6 View Debugging. I don’t like the idea of a massive storyboard with lots of ViewController in it, so I don’t look at storyboard first.

[[FLEXManager sharedManager] showExplorer];

chisel

LLDB is good, and it’s much better with chisel. It provides lots of useful commands for you to interact with views and properties.

Control-1

Xcode jumpbar menu 1 gives you some more info about class: Counterparts, Superclasses, Siblings, Includes, Included By, …

Symbolic Breakpoint

You can create symbolic breakpoint through Xcode or LLDB.

Symbolic breakpoints stop program execution when a specific function or method starts executing.
You can specify the symbol as:
A method name. For example, pathsMatchingExtensions:.
A method of a particular class. For example, [SKTLine drawHandlesInView], people::Person::name().
A function name. For example, _objc_msgForward.

ObjectGraph-Xcode

ObjectGraph can show oriented graph of dependencies between classes in your project.
There is objc_dep that worths looking

Reference

  1. Navigating a New Codebase: Tips and tricks for getting up to speed quickly
  2. Navigating and discovering an iOS codebase using lldb

217 total views, 4 views today

Handling UITextField change with symbol

In my app, there is a requirement to allow user to input value with symbol, like $45, 28 lbs, 22″, …, plus some more requirement like limiting the number of digits, validating against max and min value, showing default text when the field is empty, only showing symbol when there is at least 1 digit, …

I only want the easy way :]

There are 2 solutions that came into mind
1. Show $45. When user begins editing, remove that dollar symbol. When user ends editing, add that dollar symbol again
2. Show $45. When user edits, check and handle appropriately to keep that dollar symbol along

People prefer option #2 because it make user aware of the unit they are inputting. But it requires a lot of handling as you must separate the symbol from the digits. Some libraries prove this TSCurrencyTextField, …

There is option #3, that is to use UITextField leftView and rightView for the symbol. The idea is to determine the text property of UITextField and show/hide leftView, rightView accordingly

leftView and rightView

Let’s create a label called decoratorLabel to use as the leftView and rightView

- (void)setupDecoratorLabel {
    self.decoratorLabel = [[UILabel alloc] init];
    self.decoratorLabel.text = self.decorator;
    self.decoratorLabel.textColor = [UIColor blackColor];
    self.decoratorLabel.font = [UIFont lightFontWithSize:kProfileTextSize];

    CGSize size = [self.decoratorLabel sizeThatFits:CGSizeMake(20, CGRectGetHeight(self.textField.bounds))];
    self.decoratorLabel.frame = CGRectMake(0, 0, size.width, size.height);
}

Show and hide decoratorLabel accordingly

Subscribed to UITextFieldDelegate and UIControlEventEditingChanged event. Here I demonstrate for leftView, the same for rightView

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    [self determineToShowOrHideDecoratorLabel];
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    [self hideDecoratorLabel];
}

// This is from UIControlEventEditingChanged
- (void)textFieldEditingChanged:(UITextField *)textField {
    // Tell textField to invalidate intrinsicContentSize, so that its width is dynamic
    [textField invalidateIntrinsicContentSize];
    [self determineToShowOrHideDecoratorLabel];
}

- (void)hideDecoratorLabel {
    self.textField.leftView = nil;     
}

- (void)showDecoratorLabel {
    self.textField.leftView = self.decoratorLabel;
    self.textField.leftViewMode = UITextFieldViewModeAlways;
}

- (void)determineToShowOrHideDecoratorLabel {
    // self here is UITextField
    if (self.text.length > 0) {
        [self showDecoratorLabel];
    } else {
        [self hideDecoratorLabel];
    }
}

intrinsicContentSize

I don’t know why but UITextField only update its intrinsicContentSize when it resigns its first responder status, and its size is affected by its placeHolder text also. See Resize a UITextField while typing (by using Autolayout)

So the solution is to override intrinsicContentSize and return width based on the text

- (CGSize)intrinsicContentSize {
    if (self.isEditing) {
        CGSize size = [self.text sizeWithAttributes:self.typingAttributes];
        return CGSizeMake(size.width + self.rightView.bounds.size.width + self.leftView.bounds.size.width + 2, size.height);
    }

    return [super intrinsicContentSize];
}

Here we +2 to make room for the caret

PS: If for some reasons, you want UITextField intrinsicContenSize to take effect immediately, call textField layoutIfNeeded

Reference

  1. How to handle number text field change with symbol?
  2. Resize UITextField horizontally with text using Auto Layout
  3. UITextfield leftView/rightView padding on iOS7

202 total views, 3 views today

LLDB

  1. Navigating and discovering an iOS codebase using lldb

123 total views, 1 views today

Interview

  1. This is why you never end up hiring good developers

191 total views, 2 views today

Software engineering

  1. 15 things I wish I’d learned earlier about software engineering
  2. The Ten Commandments of Egoless Programming
  3. 6 common traits of good programmers

210 total views, 1 views today

AVPlayer and HTTP Live Streaming

149 total views, no views today

Name it right

Just as how iOS shows us how to adopt best coding practices and standards, it shows us how to name things right and consistently as well.

I take examples from UIKIt and Foundation to demonstrate, in no particular order. Also, we should avoid grammar and spelling mistakes.

performSelector:onThread:withObject:waitUntilDone:modes:

We can write performSelector:::: but here correct name parameters help us understand what each parameter requires

showAlertViewForTaskWithErrorOnCompletion:delegate

Method which has no return begins with a verb

filteredArrayUsingPredicate:

Method which returns value start with a noun, notice how filtered is. We can prefix noun with past participle and adjective.

imageNamed:

Notice Named, not Name. This is shorthand of imageWhichIsNamed
Read Reduced relative clauses

deselectRowAtIndexPath:animated:

Notice animated, not animate. animated is is used to present a state

UIView

Object is a noun, in single form

superview

Getter does not look like this getSuperview

isUserInteractionEnabled

Getter for boolean is normally prefixed with is when appropriated

NSEnumerator

An object should be named so that people understand its main and only job

NSApplicationDidBecomeActiveNotification

If it is a notification, it should have notification in its name

contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error:

Notice including..., not include

respondsToSelector:

Notice responds, not respond. Objective-C allows us to read code like English sentence, so there must be agreement between subject and verb

viewWithTag:

Don’t use taggedView, because viewWithTag allows us to specify the tag parameter more naturally.

runModalForDirectory:file:types:

Notice now we name file, not andFile. and keyword is needless

presentationControllerForPresentedViewController:presentingViewController:sourceViewController:

Long method name are more expressive

[NSArray array]

Creates and returns an empty array.

alloc

This is an abbreviation for allocate. Use abbreviations consistently

Reference

  1. Introduction to Coding Guidelines for Cocoa
  2. English Part of Speech
  3. What are the naming conventions of functions that return boolean?
  4. objective-c-style-guide by Raywenderlich team
  5. Learn Objective-C: Cocoa Naming Conventions
  6. Objective C method naming convention

278 total views, 2 views today

iOS best practices

Here are practices I found useful when developing iOS. Actually, it applies to other platform as well

Name it right

Project structure

Dependency

Single Responsibility Principle

Performance

Reference

  1. ios-good-practices by futurice
  2. What are best practices that you use when writing Objective-C and Cocoa?
  3. iOS-Best-Practices by jverkoey
  4. iOS 7 Best Practices; A Weather App Case Stud
  5. iOS Tips by Krzysztof Zabłocki

351 total views, 2 views today

iOS interview questions

I found those interview questions very interesting, time to review :]

  1. Interview Questions for iOS and Mac Developers by Daniel
  2. iOS Interview Questions by Kyle and How to Apply for an iOS Developer Job
  3. Tech Interview Questions for iOS Developers Leslie
  4. iOS Interview Questions and Answers By GeekInterview
  5. 6 Great iOS Interview Questions by Toptal
  6. iOS Interview Questions and Answers by Chetan
  7. So You Have a Technical Interview at Macoscope

377 total views, 1 views today

The importance of types

I found those posts about types very useful. Just to use types correctly and let the compiler and static analyzer catch the bugs for you.

  1. More Typing, Less Testing: TDD with Static Types, Part 1
  2. Replace Enumerations with Types
  3. The Design of Types
  4. Phantom Types
  5. Tiny Types
  6. Wrapper Types
  7. Enums instead of Booleans

287 total views, no views today

© 2015 Fantageek

Theme by Anders NorenUp ↑