内存泄漏 - 格式化字符串要显示时间,每秒 [英] Memory Leaks - Formatting a String To Display Time, Each Second

查看:154
本文介绍了内存泄漏 - 格式化字符串要显示时间,每秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们。我有一个方法每秒被调用,我想使用它来显示我的应用程序一直在做的工作的时间。目前我使用的类(其中我没有创建)有一个名为 progress 的属性,它存储总秒数。



我已经写了一些代码,这需要这些秒,并将其格式化为可读的字符串。我是新来的,所以请原谅我,如果它不是最好的代码。欢迎任何建议:

  //小时,分钟和秒是作为整数定义的实例变量
int totalSeconds = (int)streamer.progress;

小时= totalSeconds /(60 * 60);

if(hours> 0)
formattedTimeString = [NSString stringWithFormat:@%d:,hours] // WRONG

minutes =(totalSeconds / 60)%60;
seconds = totalSeconds%60;
[formattedTimeString stringByAppendingFormat:@%d:%d,minutes,seconds]; // WRONG

基本上我想让它显示为3:35,例如显示3分钟,35秒。我只想显示小时部分,如果它已经是一个小时,在这种情况下它会是2:3:35例如(任何人都可以推荐更好的方式来格式化这)。



问题我有的是我实际创建/设置字符串(标记为WRONG的行)。由于这是每秒执行,如果我不断要求一个新的字符串对象,我很容易得到一个泄漏。我想我可以通过在方法结束时释放foramttedTimeString来解决这个问题,但是这是正确的方法来实现呢? NSMutableString 会以任何方式帮助吗?有没有更好的,Cocoa的做法这样做?我已经问过 #iphonedev @ freenode,他们说我必须自己写这个方法,但我想我会再问。



提供上下文:这是一个互联网广播流式传输应用程序(我知道已经有很多,但我只是练习)。我想要能够显示流播放的时间量。



对不起,如果这个问题是愚蠢的,嘿,我说我是新来的这个。

解决方案

我会这样做:

  int totalSeconds =(int)streamer.progress; 
hours = totalSeconds /(60 * 60);
minutes =(totalSeconds / 60)%60;
seconds = totalSeconds%60;

if(hours> 0){
formattedTimeString = [NSString stringWithFormat:@%d:%02d:%02d,hours,minutes,seconds];
} else {
formattedTimeString = [NSString stringWithFormat:@%d:%02d,minutes,seconds];
}

现在最后,formattedTimeString是所需的时间,自己 - 如果你想保留它,你必须保留它,或者将它存储在一个副本属性中。



注意%02d给你一个保证两个数字,零填充数字,这通常是你想要的数字在一部分时间。



要看看你将如何使用stringByAppendingFormat, this:

  NSString * formattedTimeString = @; 
if(hours> 0){
formattedTimeString = [formattedTimeString stringByAppendingFormat:@%d:,hours];
}
formattedTimeString = [formattedTimeString stringByAppendingFormat:@%d:%02d,minutes,seconds];

但在这种情况下,你会得到像3:4:05的时间希望3:04:05。



请注意,formattedTimeString每次都会被覆盖,但这是确定的,因为你没有拥有它在任何时候,所以你



最后,要使用可变字符串查看它,它可能如下所示:

  NSMutableString * formattedTimeString = [NSMutableString string]; 
if(hours> 0){
[formattedTimeString appendFormat:@%d:,hours];
}
[formattedTimeString appendFormat:@%d:%02d,minutes,seconds];

同样,时间结果是不受欢迎的3:4:05,因此它必须保留或存储一个副本属性来保存它。


Hey guys. I have a method that gets called each second which I want to use to display the time that my app has been doing it's work. Currently the class I'm using (Which I did not create) has a property named progress which stores the total number of seconds.

I have already written some code which takes these seconds and formats it into a readable string. I'm new to this, so pardon me if it's not the best code. I welcome any suggestions:

// hours, minutes, and seconds are instance variables defined as integers
int totalSeconds = (int)streamer.progress;

hours = totalSeconds / (60 * 60);

if (hours > 0)
    formattedTimeString = [NSString stringWithFormat:@"%d:", hours]; // WRONG

minutes = (totalSeconds / 60) % 60;
seconds = totalSeconds % 60;
[formattedTimeString stringByAppendingFormat:@"%d:%d", minutes, seconds]; // WRONG

Basically I want it to appear as "3:35" for example to show 3 minutes, 35 seconds. I only want to show the hour section if it has been an hour, in which case it would be "2:3:35" for example (Can anyone recommend a better way to format this?).

The problem I am having is where I actually create/set the string (The lines tagged WRONG). Since this is being done every second, I would easily get a leak if I keep asking for a new string object. I figure I can solve this by releasing the foramttedTimeString at the end of the method, but is this the correct way to accomplish this? Would an NSMutableString help in any way? Is there a better, Cocoa way of doing this? I already asked in #iphonedev @ freenode and they said I would have to write this method myself, but I figured I'd ask again.

To provide context: this is an internet radio streaming app (I know there are many already, but I'm just practicing). I want to be able to show the amount of time the stream has been playing for.

Sorry if this question is stupid, heh, like I said I'm new to this.

解决方案

I would do it something like:

int totalSeconds = (int)streamer.progress;
hours = totalSeconds / (60 * 60);
minutes = (totalSeconds / 60) % 60;
seconds = totalSeconds % 60;

if ( hours > 0 ) {
    formattedTimeString = [NSString stringWithFormat:@"%d:%02d:%02d", hours, minutes, seconds];
} else {
    formattedTimeString = [NSString stringWithFormat:@"%d:%02d", minutes, seconds];
}

Now at the end, formattedTimeString is the desired time, but you do not "own" it - you must retain it, or store it in a "copy" property if you wish to keep it around.

Note that the %02d gives you a guarenteed two digits, zero filled number, which is usually what you want for numbers in parts of times.

To see how you would do it with stringByAppendingFormat, it would look something like this:

NSString* formattedTimeString = @"";
if ( hours > 0 ) {
    formattedTimeString = [formattedTimeString stringByAppendingFormat:@"%d:", hours];
}
formattedTimeString = [formattedTimeString stringByAppendingFormat:@"%d:%02d", minutes, seconds];

However in this case, you'll get times like 3:4:05, rether than a more desirable 3:04:05.

Note that formattedTimeString is being overwritten each time, but that is OK bvecause you do not "own" it at any time, so you are not responsible for releasing it.

Finally, to see it with a mutable string, it might look like this:

NSMutableString* formattedTimeString = [NSMutableString string];
if ( hours > 0 ) {
    [formattedTimeString appendFormat:@"%d:", hours];
}
[formattedTimeString appendFormat:@"%d:%02d", minutes, seconds];

Again, the time result is the undesirable 3:4:05, and again you do not own formattedTimeString at the end, so it must be retained or stored with a copy property to keep it around.

这篇关于内存泄漏 - 格式化字符串要显示时间,每秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆