Reputation: 604
i am using label to display data from various string but if string is not giving any value i want to display nothing in label but hare in below code it is displaying "null" if no value is given by sting. i don't want to display null in label .... how can i solve it.
where_do_you_hurt.text = [NSString stringWithFormat:@"%@,%@,%@,%@",appdelegate.hurt_head,appdelegate.hurt_Arm,
appdelegate.hurt_leg,appdelegate.hurt_chest,appdelegate.hurt_Back];
Upvotes: 3
Views: 4459
Reputation: 8463
Here is the macro that I use for safe string instead of getting "(null)" string on a UILabel for example:
#define SafeString(STRING) ([STRING length] == 0 ? @"" : STRING)
NSLog(@"%@", member.name); // prints (null) on UILabel
with macro:
NSLog(@"%@", SafeString(member.name)); // prints empty string on UILabel
nice and clean solution.
Upvotes: 1
Reputation: 811
import these file directly in your project and use rMtext instead of text property.
#import <UIKit/UIKit.h>
@interface UILabel (NullHandling)
@property(nonatomic,copy) NSString *rMtext;
@end
#import "UILabel+NullHandling.h"
#import <objc/message.h>
@implementation UILabel (NullHandling)
-(void)setRMtext:(NSString *)text {
if(text!=nil){
text = [text stringByReplacingOccurrencesOfString:@"(null)" withString:@""];
self.text=text;
}
}
-(NSString *)rMtext {
return self.text;
}
@end
Upvotes: 0
Reputation: 7517
This is how I did it. Its not as compact as the others, but its more readable I feel (which is always most important to me).
It also has the benefit of removing trailing whitespaces from the beginning and the end.
// Remove any nulls from the first or last name
firstName = [NSString stringWithFormat:@"%@", (firstName ? firstName : @"")];
lastName = [NSString stringWithFormat:@"%@", (lastName ? lastName : @"")];
// Concat the strings
fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
// Remove any trailing whitespace
fullName = NSString *newString = [oldString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
Upvotes: 0
Reputation:
u can check for null using
if (title == (id)[NSNull null] || title.length == 0 ) title = @"Something";
Upvotes: 0
Reputation: 7976
About NSString
... [NSString stringWithFormat:@"%@", nil]
will log as (null)
You could put your text together incrementally, testing for nil values.
OR
You could initialize those 4 properties in your appdelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.hurt_head = [NSString string];
self.hurt_Arm = [NSString string];
self.hurt_leg = [NSString string];
self.hurt_chest = [NSString string];
self.hurt_back = [NSString string];
//...
}
Upvotes: 0
Reputation: 92306
For each value that you don't want to have (null)
printed, you need to provide an empty string if the value is nil
. You can do so like this:
foo = [NSString stringWithFormat:@"%@", (obj ? obj : @"")];
The obj ? obj : @""
means: if the object is not nil (obj ?
) then pass the object (obj
), otherwise pass the empty string (: @""
).
An alternative would be use to a mutable string and then do:
if (obj) {
[foo appendFormat:@"%@", obj];
}
Upvotes: 16
Reputation: 24115
If you don't want "null" to appear for nil objects, then you need to supply an empty string instead:
where_do_you_hurt.text = [NSString stringWithFormat:
@"%@,%@,%@,%@",
appdelegate.hurt_head ? appdelegate.hurt_head : @"",
appdelegate.hurt_Arm ? appdelegate.hurt_Arm : @"",
appdelegate.hurt_leg ? appdelegate.hurt_leg : @"",
appdelegate.hurt_chest ? appdelegate.hurt_chest : @"",
appdelegate.hurt_Back ? appdelegate.hurt_Back : @""];
Upvotes: 2