Showing posts with label coding. Show all posts
Showing posts with label coding. Show all posts

Monday, October 21, 2013

unable to dequeue a cell with identifier



Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

To get rid of this you can do a couple things.  First make sure your tableViewCell identifier has an identifier.  In this case it would need to be "Cell".

Next, if you are using 
dequeueReusableCellWithIdentifier:forIndexPath: 
you need to register your class in viewDidLoad with:
[self.tableView registerClass: [FontCell class] forReuseIdentifier: @"Cell"];

If you don't want to do that, you can do it the old way, but you must check if your cell is nil:
dequeueReusableCellWithIdentifier:cellIdentifier



Tuesday, October 15, 2013

Subclassing UILocalNotification does not work

When I initialize a subclass of UILocalNotification like so:

Noticiation* _notification = [[Notification alloc] init];
NSLog(@"%@", [_notification class]);

It prints out UIConcreteLocalNotification.  Trying to call methods or set properties on the subclass will result in an unrecognized selector error.

It seems that UILocalNotification is an abstract class which is implemented by the private class UIConcreteLocalNotification.

http://stackoverflow.com/questions/8580782/possible-to-subclass-uilocalnotification-and-change-default-close-button-text

Tuesday, October 8, 2013

iOS 7 Lets you Choose User-Set System Fonts


By using something like:
self.body.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
You can have the fonts in your app exactly how the user prefers.

http://stackoverflow.com/questions/18951332/how-to-detect-dynamic-font-size-changes-from-ios-settings

Thursday, June 30, 2011

viewWithTag and UIWebview

Be careful when you have a UIWebview as a subview and you are trying to get a view by using viewWithTag.  The UIWebview has subviews of its own and can have the tag you are looking for.  So you may end up with a UIScrollview instead of a UIWebview.  It's best to check what class the view you are getting back is.

Tuesday, April 19, 2011

static const or #define?

What is the difference?  Which one should I use?  According to this post, static const is generally the best choice.
http://stackoverflow.com/questions/1674032/static-const-vs-define-in-c

Sunday, February 6, 2011

Continue a String onto the next line

If you have a long string you may want to split it up into multiple lines to make it easier to read.  To do this, you can do the following:


    NSString * const TEXT = @"This is the first of many words"
                             " and will continue here";

Tuesday, July 13, 2010

How to check if bool is null.


A handy way to check if a stored bool value does not exist.  originally from http://stackoverflow.com/questions/2760112/how-to-check-if-a-bool-is-null
You can test first and assign then conditionally, e.g. something like the following:
if (NSValue* val = [results objectForKey:@"current_user_following"]) {
    mySTUser.current_user_following = [val boolValue];
}
This:
  • avoids calling objectForKey: twice by storing the result in a variable
  • limits the scope of the variable to the if statement
  • works because nil is is equivalent to 0
  • thus only executes the assignment statement if val is not nil
To additionally check for the value being NSNull you'd have to add another test as given by ChristopheD, but i question wether NSNull is really needed here - YES/NO should be sufficient for a description like "is following".
If you have no useful value for a key, you could simply remove it from the dictionary or not insert it in the first place.

Monday, July 5, 2010

How to get the class of an object.

Use this to:
  • determine if the object is a instance of a NSNumber or other Class
  • if the object is nil or if it's a NSNumber or other Class
if ([obj class] == [NSNumber class])

or use this to determine if the object is a kind of class
if ([obj isKindOfClass:[NSNumber class]])

Friday, May 28, 2010

'TheClassName' may not respond to '-methodName:forKey:withType'

// file.m
- (void)methodName:(id)value forKey:(NSString*)key {
[self methodName:value forKey:key withType:A_TYPE];
}

- (void)methodName:(id)value forKey:(NSString*)key withType:(TYPES)type {
....
}

The above lines will show a warning similar to 'TheClassName' may not respond to '-methodName:forkey:withType'. This is because of the order that the methods are written. When the compiler encounters the first method it flags it because the second method has not yet been defined. Reorder the methods to avoid this annoying warning.


Another reason this warning message might show is because the method actually is not defined or cannot be found in the interface file. I had the above method declared in a protocol, not the actual interface file. I imported the protocol, but forgot to tell the class to use the protocol ( i didn't say "@interface TheClassName <TheProtocolName> "). I found this out by doing a right-click -> Jump to Definition. And it gave me two options (one in the protocol and one in the class) instead of just going to the Protocol's declaration.

Tuesday, May 4, 2010

Why I do i have to nil out an object to re-use it?

I'm using a singleton object in my app. The object needs to be re-set and basically recreated when the user chooses to do so. Right now I have the following code so that it works correctly.

if (obj == nil) {
[obj release];
obj = nil;
}
obj = [[self alloc] init:param];


Answer:
I found that obj already has it's memory allocated, so it does not need alloc anymore. To reuse the object, only the init method needs to be called... Instead of re initializing the object everytime, the following can be done:

+ (Obj*)sharedManager:(NSString*)param {
if (obj == nil) {

obj = [[self alloc] init];

}
[obj setAllParameters:param];
}

So basically, it will allocate the object only once. And it will always set the parameters. So simple! I feel like a true iphone idoit developer.