Best Methode to handle NSString Memeory [closed] - ios

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I am trying to optimise my app to the as best as it can get, can you please suggest which method is best, and recommended.
#implementation Methode1
+(BOOL)Isdone{
BOOL result = [[NSUserDefaults standardUserDefaults] boolForKey:#"DEVICE_TYPE"];
if(!result){
[[NSUserDefaults standardUserDefaults]setBool:YES forKey:#"DEVICE_TYPE"];
}
return result;
}
#end
#implementation Methode2
NSString * const deviceTypeKey #"DEVICE_TYPE";
+(BOOL)Isdone{
BOOL result = [[NSUserDefaults standardUserDefaults] boolForKey:deviceTypeKey];
if(!result){
[[NSUserDefaults standardUserDefaults]setBool:YES forKey:deviceTypeKey];
}
return result;
}
#end
#implementation Methode3
#define deviceTypeKey #"DEVICE_TYPE"
+(BOOL)Isdone{
BOOL result = [[NSUserDefaults standardUserDefaults] boolForKey:deviceTypeKey];
if(!result){
[[NSUserDefaults standardUserDefaults]setBool:YES forKey:deviceTypeKey];
}
return result;
}
#end
in the three above mentioned method which one is most memory efficient and why?

None of them will make the slightest significant difference in terms of memory. Constant string literals are optimised by the compiler for you.
What's more important is the risk of programmer errors and maintainability - so using a constant or a define for your defaults keys is the way to go. I'm a constants fan myself.
Also - see #JustSid's comment. You need to use instruments to look for genuine problems, don't pick random bits of code and agonise over them. Write for mainainabilty and readability first, then find actual problems by profiling.

Related

How to check if a string changes [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am quite new in iOS programming but I need it to test out a prototype.
I am receiving a value over bluetooth, which changes when a button is pressed on the physical prototype.
In my app a action has to follow when the button is pressed, therefore I need to know when the value has changed.
I have tried everything I could think of and looked everywhere but found no solution.
This is my current code:
-(void)bean:(PTDBean *)bean didUpdateScratchNumber:(NSNumber *)number withValue:(NSData *)data{
int value = *(int*)([data bytes]);
NSString *lastvalue = nil;
NSString *newValue = [NSString stringWithFormat:#"%d", value];
NSLog(#"ScratchWaarde: %d", value);
if ([newValue isEqualToString:lastvalue]) {
NSLog(#"Last Value: %#", lastvalue);
}else{
NSLog(#"Pushed");
lastvalue = newValue;
}
}
Thank you for helping!
Yeah, it looks like the OP expects lastValue to be kept as state on the object, so...
// in this class's interface
#property(strong,nonatomic) NSString *lastValue;
-(void)bean:(PTDBean *)bean didUpdateScratchNumber:(NSNumber *)number withValue:(NSData *)data{
// it looks like the OP is trying to get a string from the data, so...
NSString *newValue = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"ScratchWaarde: %d", value);
if ([newValue isEqualToString:self.lastvalue]) {
NSLog(#"Last Value: %#", self.lastvalue);
} else {
NSLog(#"Pushed");
self.lastvalue = newValue;
}
}
You code makes no sense. You are saying:
NSString *lastvalue = nil;
NSString *newValue = // ...
if ([newValue isEqualToString:lastvalue]) {
Under no circumstances will newValue ever be equal to lastValue, because lastValue is guaranteed to be nil, because that is the value you are setting it to in the first line (and newValue, because you are setting it to an actual NSString, is guaranteed not to be nil).
So, while I'm not clear on what exactly you are trying to accomplish, this obviously isn't it. It's not so much a programming matter as a matter of simple logical thought. The program can only do what you tell it to do, and what you are telling it to do is silly.
(In all probability what you are not grasping is that lastvalue is purely local to this method, so it is new every time your method is called. If you want a persistent lastvalue, you need a property global to the class, not a local variable.)

What's wrong with this code? I'm trying to change a label text with NSString and NSLog [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to do something very simple, I'm trying to change a label to "I like to say hi" by clicking a button. I already know how to do it without NSString but I wanted to make it more harder, but I can't find what's wrong with this code. It's not working.
- (IBAction)button:(id)sender{
NSString *hi = #"hi";
_label.text = NSLog(#"I like to say %#", hi);
}
I'm also trying to do something even more complicated and use a void statement like this. Would this work, and if not what's wrong with it?
- (void)num{
int num = 42;
}
- (IBAction)button:(id)sender{
_label.text = NSLog(#"I like to say %i", num);
}
Why are you trying to use NSLog for this? That's for showing messages in the log.
You want NSString stringWithFormat:.
_label.text = [NSString stringWithFormat:#"I like to say %#", hi);
What you have won't even compile since the NSLog function does not return an NSString value.
NSLog is a function with a void return type, which means it does not return any value, so you can't use it to assign the value of a property. It is used only for printing messages to the debugging console or device logs. What you are probably looking for is stringWithFormat:
_label.text = [NSString stringWithFormat:#"I like to say %#", hi];
This is a method of the NSString class. It constructs strings similar to NSLog, but it actually returns a new NSString value that can be assigned to properties and so on.
Read its documentation here. You will be using this and other NSString methods all the time.
Regarding your other code:
If you want to construct a method named num that produces a value you can use elsewhere, you need to declare the type of the value it returns, instead of using void:
- (int)num{
return 42;
}
Now num is a proper instance method. You will need to tweak your code to be able to access this method correctly:
- (IBAction)button:(id)sender{
_label.text = [NSString stringWithFormat:#"I like to say %i", [self num]];
}
Here you are sending the message num to self. The keyword self is, loosely speaking, a reference to the current object instance. So [self num] will invoke the num method you have defined, and return its value. You may want to read Programming with Objective-C to learn more about these concepts.

Setting BOOLEAN in +(void) issue [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I am trying to call BOOL in +(void) method but I can't. Why it would not set in +(void) method? While it is working on all -(void) methods.
.h
#property (nonatomic, assign) BOOL line;
.m
+ (void)normalizeCell:(UITableViewCell *)cell withLables:(NSArray *)lbls sx:(CGFloat)sx widths:(NSArray *)widths
if (![cell.contentView viewWithTag:22])
{
UIImageView *gb = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"table.png"]];
gb = 22;
[cell.contentView addSubview:gb];
[gb release];
}
You can't use your header property in class method (+), only in instance method (-).
This is some kind of static method known from other programming language. In objective-c you can use class method to do some operation which fit to class you've created, but you have to remember that using a class method is NOT an object operation. You don't have to creating objects to use class method and of course you can't access properties of the objects.
+ methods are class level methods and properties are instance level variables. Therefor it's not possible to set them, on what instance would they be set? Class level methods should not be used if you need to keep state. You can keep state if you really want to like this.
+ (BOOL)myBool:(NSNumber *)boolValue{
static BOOL myBool = false;
if (boolValue){
myBool = [boolValue boolValue];
}
return myBool;
}
If you want this to not be part of the public interface just put this directly in the .m file so it's invisible for other classes. Then when you are inside your other class method you can do.
BOOL b = [self myBool:nil]; // get value
[self myBool:[NSNumber numberWithBool:YES]]; // set value
If you for reason would like to access this from your instances you can like this.
BOOL b = [MyClass myBool:nil];
[MyClass myBool:[NSNumber numberWithBool:NO]];

Dynamic cast to a custom class in objective C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
In NewTaskViewController.h, delegate was declared to be a property of type id.
Does the typecast below make delegate point to the object of type ViewController?
#import "NewTaskViewController.h"
#import "ViewController.h"
#implementation NewTaskViewController
- (IBAction)saveTask:(id)sender {
if ([self.textField.text length] == 0)
return;
ViewController *tasksListView = (ViewController *)self.delegate;
[tasksListView.tasks addObject:self.textField.text];
[self close:sender];
}
- (IBAction)close:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
#end
The cast tells the compiler that you want to use the delegate object as if it were a ViewController. If it really is one, you're OK. If it's not, bad things will happen at run-time. That is, the cast doesn't do any kind of conversion.

Objective-c implement and call method [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I've tried to reproduce an example from this question:
How to create a method in Objective-C that takes a NSString stringWithFormat as a parameter?
And I have created in #interface:
- (float)strToFloat:(NSString *)str;
And in #implementation:
- (float)strToFloat:(NSString *)str {
str = [str stringByReplacingOccurrencesOfString:#"."
withString:#""];
str = [str stringByReplacingOccurrencesOfString:#","
withString:#"."];
return [str floatValue];
}
After all when I try to compile the app I receive this error: Use of undeclared identifier 'strToFloat' in this method:
- (IBAction)addItem:(id)sender {
NSLog(#"float value is: %.2f", [strToFloat labelPrice.text]);
}
What's wrong with this example?
You have to call the method correctly:
NSLog(#"float value is: %.2f", [self strToFloat:labelPrice.text]);
I'm assuming your addItem: and strToFloat: methods are in the same class.
BTW - there are MUCH better ways to convert decimal string values to numbers that deal with a user's locale. Make use of NSNumberFormatter.

Resources