Tyron
Tyron

Reputation: 1976

How to DRY code in iOS / Objective C?

I've been programming on iOS for a few months and I think I haven't quite grasped all the concepts of Objective-C yet. Hence, I shudder every time I have to copy code between Objective-C Classes and between Projects, simply because I currently don't know better.

Just 2 examples:

  1. For some of my UITextField's I require to show a pickerview instead of the standard keyboard. So I made myself a function makePicker that initializes a pickerview with a button, etc. If I have another textfield in another class I would need to copy that function over, as I couldn't find a way to share functions like these accross classes in a clean way. What would be a clean approach to this issue?

  2. A lot of my classes need to read&write JSON to a http url, which means I have to decode and encode data as well as implement everything of NSURLConnectionDelegate. I would sincerly love to have a component that handles all these callbacks for me, instead of having them spread all over the place.

Thank you in advance for your inputs!

Upvotes: 0

Views: 456

Answers (5)

Armand DOHM
Armand DOHM

Reputation: 1131

1/ I think your request is more POO than just iOS or Objective-C... If you need an object with more function than what the original have, just create a new class inherited from the original and add your function. Now use this new class and you'll have access to the function. Of course with Objective-c you also have the option of category if you don't need your own data to be saved in the object.

PickerTextField.h

@interface PickerTextField : UITextField 
@end

PickerTextField.m

@implementation PickerTextField

- (BOOL)becomeFirstResponder {
  self.inputView = YOUR-PICKER;  //YOUR-PICKER have to be create first !!!
  return [super becomeFirstResponder];
}

- (BOOL)resignFirstResponder {
    self.inputView = nil;
    return [super resignFirstResponder];
}

@end

2/ May be you need to implement the SINGLETON protocol for this, isn't it ?

Upvotes: 1

lancy
lancy

Reputation: 857

Q1.

Way1: There are two properties inputView and inputAccessoryView in UITextField, so create your own class of InputView, something like DatePickerInputView, bind with the textField, change the TextField.text when the picker pick a date.

Way2: Make your own subclass of UITextField, write the code in it.

Way3: Wirte a Utils static methods, take the textField as the parameters, so you can share your code.

Q2.

There are so many network-libraries do the stuff as your asking, like AFNetworking or ASIHTTPRequest, check out the source code, if you want to figure out how it happened.

Upvotes: 0

Sepehrom
Sepehrom

Reputation: 1335

For your second question, you can take advantage of SBJson framework which can be downloaded from following URL :

https://github.com/stig/json-framework/downloads

Hope it help you ;)

Upvotes: 0

zaph
zaph

Reputation: 112855

  1. Create a class. If you don't need ivars make the methods class methods. If you do make it a subclass, in this case a subclass of UITextField.

  2. The point of a delegate is that it can be a different class. Create a delegate class and possibly sub-classes for the delegate.

Upvotes: 1

Gabriele Petronella
Gabriele Petronella

Reputation: 108159

  1. Make your own subclass of UITextField, with custom initialization

  2. AFNetworking or - if you feel adventurous - RestKit

Upvotes: 1

Related Questions