Iphone 自定义类中的NSString->;已添加到字典->;何时获取的NSString值已更改?
我正在尝试使用OpenAL在iphone上传输文件。这段代码大部分是基于;但是,我尝试将数据存储在字典中的自定义类中,而不是字典中的字典中 我已经设置了自定义类来保存关于我将要流式处理的文件的信息。这里包含一个名为fileName的NSString,它保存我的文件的位置。正是该变量在检索时更改其值。自定义类的定义如下:Iphone 自定义类中的NSString->;已添加到字典->;何时获取的NSString值已更改?,iphone,streaming,nsmutabledictionary,openal,Iphone,Streaming,Nsmutabledictionary,Openal,我正在尝试使用OpenAL在iphone上传输文件。这段代码大部分是基于;但是,我尝试将数据存储在字典中的自定义类中,而不是字典中的字典中 我已经设置了自定义类来保存关于我将要流式处理的文件的信息。这里包含一个名为fileName的NSString,它保存我的文件的位置。正是该变量在检索时更改其值。自定义类的定义如下: @interface DBuffer : NSObject { NSString *fileName ; UInt32 fileSize
@interface DBuffer : NSObject {
NSString *fileName ;
UInt32 fileSize ;
UInt32 bufferSize ;
UInt32 bufferIndex ;
ALenum bufferFormat ;
ALsizei bufferFreq ;
BOOL bufferLoops ;
}
@property (nonatomic, retain) NSString *fileName ; *<-----;
@property (nonatomic, assign) UInt32 fileSize ;
@property (nonatomic, assign) UInt32 bufferSize ;
@property (nonatomic, assign) UInt32 bufferIndex ;
@property (nonatomic, assign) ALenum bufferFormat ;
@property (nonatomic, assign) ALsizei bufferFreq ;
@property (nonatomic, assign) BOOL bufferLoops ;
-(void)dealloc ;
@end
@implementation DBuffer
@synthesize fileName ;
@synthesize fileSize ;
@synthesize bufferSize ;
@synthesize bufferIndex ;
@synthesize bufferFormat ;
@synthesize bufferFreq ;
@synthesize bufferLoops ;
-(void)dealloc
{
[fileName release];
[super dealloc];
}
@end
@接口DBuffer:NSObject{
NSString*文件名;
UInt32文件大小;
UInt32缓冲区大小;
UInt32缓冲指数;
阿仑缓冲格式;
ALsizei bufferFreq;
布尔缓冲环;
}
@属性(非原子,保留)NSString*文件名*
在loadFiles
中,您覆盖了fileName
和fileName2
应该声明fileName
属性copy
,而不是retain
在loadFiles
中,您覆盖了fileName
和fileName2
应该声明fileName
属性copy
,而不是retain
这个代码有很多问题,表明重新读取它会有帮助(特别是看起来你有点C++经验,并基于这些知识来做假设)。在我编写Objective-C代码的最初几年中,我每隔几个月重新阅读一次该文档;非常有用
特别是:
&*retrieve.fileName
实际上什么都不做。放下&*
retainCount
没有用。对象的保留计数是一个内部实现细节。将保留计数纯粹视为三角洲;如果使其增加,则必须在处理完对象后使其减少
- (正如尼古拉所说)您的
fileName
和fileName2
变量被过度释放,可能导致崩溃。而且fileName
属性应该是copy
,而不是retain
。团队在讨论保留/发布细节方面做得很好
<>我敢打赌“构建和分析”会捕获大多数的保留发布问题。 这个代码有很多问题,表明重新读取它会有帮助(特别是看起来你有点C++经验,并基于这些知识来做假设)。在我编写Objective-C代码的最初几年中,我每隔几个月重新阅读一次该文档;非常有用
特别是:
&*retrieve.fileName
实际上什么都不做。放下&*
retainCount
没有用。对象的保留计数是一个内部实现细节。将保留计数纯粹视为三角洲;如果使其增加,则必须在处理完对象后使其减少
- (正如尼古拉所说)您的
fileName
和fileName2
变量被过度释放,可能导致崩溃。而且fileName
属性应该是copy
,而不是retain
。团队在讨论保留/发布细节方面做得很好
我敢打赌,“构建和分析”会抓住大部分/所有的保留发布问题
-(void)loadFiles
{
NSMutableDictionary* tempLibrary = [[NSMutableDictionary alloc] init];
NSString* fileName = [[NSBundle mainBundle] pathForResource:@"b2" ofType:@"caf"];
[tempLibrary setObject:[self initializeStreamFromFile: fileName format:AL_FORMAT_STEREO16 freq:44100 ] forKey: @"101" ];
[fileName release];
NSString* fileName2 = [[NSBundle mainBundle] pathForResource:@"a5" ofType:@"caf"];
[tempLibrary setObject:[self initializeStreamFromFile: fileName2 format:AL_FORMAT_STEREO16 freq:44100 ] forKey: @"102" ];
[fileName2 release];
self.soundLibrary = tempLibrary;
[tempLibrary release];
NSLog(@"load files: tempLibrary %@<%x>", tempLibrary, &*tempLibrary);
NSLog(@"load files: soundLibrary %@<%x>", soundLibrary, &*soundLibrary);
NSLog(@"load files: soundLibrary retaincount %d", [soundLibrary retainCount]);
NSLog(@"load files: tempLibrary retaincount %d", [tempLibrary retainCount]);
DBuffer *retrieve = [soundLibrary objectForKey:@"101"];
NSLog(@"retrieve: %@",retrieve.fileName);
NSLog(@"retrieve retaincount: %d",[retrieve.fileName retainCount]);
}
// this queues up the specified file for streaming
-(DBuffer*)initializeStreamFromFile:(NSString*)fileName format:(ALenum)format freq:(ALsizei)freq
{
// first, open the file
AudioFileID fileID = [self openAudioFile:fileName];
// find out how big the actual audio data is
UInt32 fileSize = [self audioFileSize:fileID];
UInt32 bufferSize = kOPENAL_STREAMING_BUFFER_SIZE;
UInt32 bufferIndex = 0;
DBuffer* DBufferID = [[[DBuffer alloc] init] autorelease];
DBufferID.fileName = fileName ;
DBufferID.fileSize = fileSize ;
DBufferID.bufferSize = bufferSize ;
DBufferID.bufferIndex = bufferIndex ;
DBufferID.bufferFormat = format ;
DBufferID.bufferFreq = freq ;
NSLog(@"DBufferID initialised %@<%x>", DBufferID, &*DBufferID);
//output of this is: "DBufferID initialised <DBuffer: 0x533a5f0><533a5f0>"
NSLog(@"DBufferID.fileName initialised %@<%x>", DBufferID.fileName, &*DBufferID.fileName);
//output of this is: "DBufferID.fileName initialised /Users/david/Library/Application Support/iPhone Simulator/4.2/Applications/7DA24283-8D58-49B8-BBEB-48B08D921367/OpenALRefine.app/b2.caf<55453a0>"
NSLog(@"DBufferID.fileName retaincount %d", [DBufferID.fileName retainCount]);
//output of this is: "DBufferID.fileName retaincount 3"
AudioFileClose(fileID);
return DBufferID;
}
- (NSUInteger)playStream:(NSString*)soundKey gain:(ALfloat)gain pitch:(ALfloat)pitch loops:(BOOL)loops
{
NSLog(@"PlayStream activiated");
ALenum err = alGetError(); // clear error code
NSLog(@"soundLibrary retaincount %d", [soundLibrary retainCount]);
DBuffer *retrieve = [soundLibrary objectForKey:@"101"];
NSLog(@"retrieve initialised %@<%x>", retrieve, &*retrieve);
NSLog(@"retrieve.fileName initialised %@<%x>", retrieve.fileName, &*retrieve.fileName);