Understand NSAssert (and its stdlib equivalent Assert)

Assert is to make sure a value is what its supposed to be. If an assertion fails that means something went wrong and so the app quits.

It is important to make sure that there is no data loss in any such situation.

Note that XCode 4 has NS_BLOCK_ASSERTIONS defined by default in release configurations. I guess if you don’t change that your released code will not contain NSAsserts. So, just put the macro in your distribution target [only].

NSAssert (and its stdlib equivalent Assert) can be very useful for debugging/unit testing, and also when you provide frameworks to stop the users from doing “evil” things. So not only does it safe-guard against potentially bad inputs but it logs them in a useful, standard way.

Assertions are commonly used to enforce the intended use of a particular method or piece of logic. They enforce that your code is used only as intended.

The value that’s passed is entered by the user, you need to do proper validation of the input rather than relying on the assertion. A user entering a value that is not allowed should be followed by a UI error, not NSAssert crashing the application.

NSAssert is to throw an exception, which you can catch and handle (try/catch).

KVO crash NSKVODeallocate

This crash happens when an instance gets released before all key value observers pointing to its properties release. You need to call removeObserver:forKeyPath: before releasing the instance.

In another scenario, it will crash if you call removeObserver:forKeyPath: on an instance if none of the key value observers are pointing to its properties.

Simulate different network conditions on iOS simulator

When enabled, the Network Link Conditioner can change the network environment of the iPhone Simulator according to one of the built-in presets:
  • EDGE
  • 3G
  • DSL
  • WiFi
  • High Latency DNS
  • Very Bad Network
  • 100% Loss

You can control the internet downlink/uplink bandwidth. BitTorrent/UTorrent some times won’t respect the upload limit so network-link-conditioner will control it.

how to uninstall unnecessary simulators in Xcode?

You can find and delete the simulators from the path /Library/Developer/CoreSimulator/Profiles/Runtimes. Restart Xcode in order to take effect.

or

You can delete a simulator from Xcode by navigation through Menu > Window > Devices > Simulators.

Change UITextField Placeholder Color

if ([textField respondsToSelector:@selector(setAttributedPlaceholder:)])
{
textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:textField.placeholder attributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}

Multiple methods named found with mismatched result parameter type or attributes

In one scenario, if the factory method that creates the object has return type “id” then the compiler will check the method signature in all the classes. If compiler find the same method signature in more than one class then it will raise the issue. So replace the return type “id” with “specific class name”.

Convert iPad xib to iPhone version

We can go with the following approach to get iPhone version of iPad xibs already exist in the project. With this approach we no need to create a new xib and manually add all the subviews.

1. Xcode recognises the iPhone version of a XIB if the suffix is ~iphone (lowercase only) and iPad version if its ~ipad. If no suffix exists then it will consider it as an iPhone version. Lets suffix all the existing xib names with ~ipad.

2. Duplicate all the xib files and open them in TextEditor and do the following.

change “com.apple.InterfaceBuilder3.CocoaTouch.iPad.XIB” to “com.apple.InterfaceBuilder3.CocoaTouch.XIB”
change “iOS.CocoaTouch.iPad” to “iOS.CocoaTouch”
change iPad size (width=”1024″ height=“768”) to iPhone size (width=“320″ height=“568″)

3. Add the xib to the project and open it on Xcode.

4. Select the root view and go to the attributes inspector.

5. Change the orientation to portrait and size to Retina 4-inch Full Screen.

6. Adjust the frame of all the subviews.

Follow

Get every new post delivered to your Inbox.

Join 142 other followers