小编fuz*_*oat的帖子

在initWithCoder中使用retain?

我正在阅读有关编码和解码的内容,我注意到有时人们会错过最后的保留,我也注意到保留有时用在一些可用的而不是其他的.请问 ...

(1)这种保留的目的是什么?为什么有时不需要?

(2)保留的使用是否意味着我需要将其与释放相匹配,如果是这样的话?

- (id) initWithCoder: (NSCoder *) decoder {
    name  = [[decoder decodeObjectForKey: @"CardName"] retain];
    email = [[decoder decodeObjectForKey: @"CardEmail"] retain];
}
Run Code Online (Sandbox Code Playgroud)

要么

- (id) initWithCoder: (NSCoder *) decoder {
    name  = [decoder decodeObjectForKey: @"CardName"];
    email = [decoder decodeObjectForKey: @"CardEmail"];
}
Run Code Online (Sandbox Code Playgroud)

加里

cocoa objective-c

10
推荐指数
2
解决办法
2765
查看次数

在Objective-C中交换Endianness?

我需要交换一些值的字节顺序,只是想知道Objective-C中是否有可用的东西,目前我用下面的CStyle函数交换字节(显然我可以重用)我只是想检查一下没有具体的我失踪?

float floatFlip(float inFloat) {
    union {
        int intValue;
        float newFloat;
    } inData, outData;

    inData.newFloat = inFloat;
    outData.intValue = CFSwapInt32BigToHost(inData.intValue);
    return(outData.newFloat);
}
Run Code Online (Sandbox Code Playgroud)

EDIT_001

感谢这里的指针,我有整数排序,最简单的交换浮点数的方法是什么?

int myInteger = CFSwapInt32BigToHost(myInteger);
Run Code Online (Sandbox Code Playgroud)

(以上代码更新)

float myFloat = floatFlip(myFloat);
Run Code Online (Sandbox Code Playgroud)

加里

objective-c endianness

10
推荐指数
2
解决办法
6379
查看次数

重复NSTimer,弱参考,拥有参考或iVar?

我想我会把这个作为一个单独的问题从我之前的 保留 - 重复 - 时间 - 后续访问中解决,因为讨论已经向前发展,使得一个新问题比另一个编辑更清晰:

场景是一个对象创建一个重复的NSTimer,让我们说在viewDidLoad中,一旦创建了NSTimer需要留在那里,所以它可以通过其他方法访问.

NSTimer *ti = [NSTimer scheduledTimerWithTimeInterval:1 
                                               target:self 
                                             selector:@selector(updateDisplay:) 
                                             userInfo:nil 
                                              repeats:YES];
Run Code Online (Sandbox Code Playgroud)

我知道在创建时,runloop取得了NSTimer的所有权,最终在[ti invalidate];调用时停止,删除和释放NSTimer .

由于我们需要以多种方法访问NSTimer,我们需要一些方法来保存参考以供将来使用,修订后的问题是:

// (1) Should the NSTimer be held using an owning reference (i.e.)
@property(nonatomic, retain) NSTimer *walkTimer;
[self setWalkTimer: ti];
...
...
// Cancel method
[[self walkTimer] invalidate;
[self setWalkTimer:nil];
...
...
// dealloc method
[walkTimer release];
[super dealloc];
Run Code Online (Sandbox Code Playgroud)

.

// (2) Should the NSTimer be held using a weak reference (i.e.)
@property(nonatomic, assign) NSTimer *walkTimer; …
Run Code Online (Sandbox Code Playgroud)

iphone cocoa-touch objective-c nstimer

10
推荐指数
1
解决办法
4530
查看次数

如何在每次构建之前清理项目?

是否有一种方法(可能使用方案)在Xcode中指定在执行新构建之前自动完成清理.

我有一个项目,有时无法建立,除非我先干净,目前我正在手工完成.

xcode

10
推荐指数
1
解决办法
3611
查看次数

stringByAppendingPathComponent,它有效吗?

EDIT_v002

我看了所有的评论,我开始看到我应该做什么.为此我修改了我的代码(见下文)我已将newPath更改为NSString,删除了[[alloc] init]和end [release],因为它现在由系统处理.我正在使用stringByAppendingPathComponent,让它在rootPath和fileName分配给NSString之前添加一个分隔符.它确实有效,我通过静态分析仪运行它没有任何问题.

// ------------------------------------------------------------------- **
// DISC: FILEWALKER ..... (cocoa_fileWalker.m)
// DESC: List all "*.png" files in specified directory
// ------------------------------------------------------------------- **
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSString *fileName;
    NSDictionary *attrDir;
    NSError *myError;
    NSNumber *fileSize;
    NSFileManager *manager = [NSFileManager defaultManager];
    NSString *rootPath = [@"~/Pictures/Ren/PRMan" stringByExpandingTildeInPath];
    NSString *newPath;

    NSLog(@"Home: %@",rootPath);

    for(fileName in [manager enumeratorAtPath:rootPath]){
        if ([[fileName pathExtension] isEqual:@"png"]) {    

            newPath = [rootPath stringByAppendingPathComponent:fileName];   
            attrDir = [manager …
Run Code Online (Sandbox Code Playgroud)

cocoa objective-c

9
推荐指数
3
解决办法
3万
查看次数

copyWithZone设置实例变量?

copyWithZone(见下文)是否正确,特别是我使用setter来填充新对象的实例变量的位?

@interface Planet : NSObject <NSCopying>
{
    NSString *name;
    NSString *type;
    NSNumber *mass;
    int index;
}
@property(copy) NSString *name;
@property(copy) NSString *type;
@property(retain) NSNumber *mass;
@property(assign) int index;
-(void)display;
@end

-(id) copyWithZone: (NSZone *) zone {
    Planet *newPlanet = [[Planet allocWithZone:zone] init];
    NSLog(@"_copy: %@", [newPlanet self]);
    [newPlanet setName:name];
    [newPlanet setType:type];
    [newPlanet setMass:mass];
    [newPlanet setIndex:index];
    return(newPlanet);
}
Run Code Online (Sandbox Code Playgroud)

EDIT_001:

这是一个更好的方法吗?

-(id) copyWithZone: (NSZone *) zone {
    Planet *newPlanet = [[[self class] allocWithZone:zone] init];
    [newPlanet setName:[self name]];
    [newPlanet setType:[self type]];
    [newPlanet setMass:[self mass]]; …
Run Code Online (Sandbox Code Playgroud)

objective-c

9
推荐指数
2
解决办法
2万
查看次数

dealloc会在iPhone上调用吗?

我注意到在学习CocoaTouch过去几周的工作原理dealloc时,当我在iPhone模拟器中退出应用程序时,似乎没有调用方法.现在,公平地说,我没有做任何过于科学的事情 - 只需加入NSLog可以打印到控制台的语句.我的问题是:

  1. 模拟器是否在应用程序出口处与Xcode断开连接,阻止我NSLog回显到控制台?

  2. 是dealloc不是被称为优化,因为应用程序正在退出?

  3. 请问dealloc,当应用程序是在实际的iPhone硬件上运行的小号被调用?

iphone cocoa-touch objective-c

9
推荐指数
1
解决办法
1729
查看次数

CLLocationManager distanceFilter是否做了什么来节省电力?

CLLocationManager distanceFilter属性是否可以在所需距离之间执行任何操作以节省电池寿命/功耗.我不认为它,但我只是想检查我是否仍然应该调用stopUpdatingLocation而不是让它继续运行.

我在这里找到一个答案,建议电话拨打它的轮询(直到达到所需的距离),从而节省电力.但另一方面,它似乎暗示即使检查所需的距离也需要天线有效,因此节省了很少的电力.

目前,当我需要位置详细信息时,我明确地执行startUpdatingLocation,在将stopUpdatingLocation消息发送到CLLocationManager之前检查合适的位置.

iphone cocoa-touch objective-c

9
推荐指数
2
解决办法
2440
查看次数

Swift Dictionary [String:String]到NSMutableDictionary?

我正在尝试创建并在属性中分配Swift Dictionary类型[String : String],这需要一个.当我尝试这个时,我得到错误:SKSpriteNodeuserDataNSMutableDictionary

'[String : String]' is not convertible to 'NSMutableDictionary'
Run Code Online (Sandbox Code Playgroud)

谁能指出我正确的方向?

    // USER DATA
    var dictionary: [String : String] = Dictionary()
    dictionary["Orbit"] = orbit
    dictionary["Zone"] = zone
    dictionary["Impact"] = impact
    var foundationDictionary = dictionary as NSMutableDictionary
    neoSprite.userData = foundationDictionary
Run Code Online (Sandbox Code Playgroud)

dictionary nsmutabledictionary sprite-kit swift

9
推荐指数
1
解决办法
1万
查看次数

NSKeyedArchiver写XML(或其他人类可读的)?

我一直试图让NSKeyedArchiver以人类可读的形式写出我的数据(即不是二进制文件)我明白我可以使用...

setOutputFormat: NSPropertyListXMLFormat_v1_0
Run Code Online (Sandbox Code Playgroud)

但是......我似乎无法正确理解语法,有人能指出我正确的方向吗?

NSData *artistData;

NSLog(@"(*) - Save All");
artistData = [NSKeyedArchiver archivedDataWithRootObject:artistCollection ];
[artistData writeToFile:@"/Users/fgx/Desktop/stuff" atomically:YES];
Run Code Online (Sandbox Code Playgroud)

编辑

我添加了setOutputFormat(见下文)但是我在编译时收到警告,我错过了什么?

warning: NSData  may not respond to 'setOutputFormat:'
Run Code Online (Sandbox Code Playgroud)

这是我使用的代码.

NSLog(@"(*) - Save All");
artistData = [NSKeyedArchiver archivedDataWithRootObject:artistCollection ];
[artistData setOutputFormat: NSPropertyListXMLFormat_v1_0];
[artistData writeToFile:@"/Users/fgx/Desktop/stuff" atomically:YES];
Run Code Online (Sandbox Code Playgroud)

加里

cocoa objective-c

8
推荐指数
1
解决办法
4062
查看次数