尝试在实例方法中 NSLog NSNumber ivar

发布于 2024-08-17 16:17:53 字数 1637 浏览 10 评论 0原文

我正在开发一个跟踪不同歌曲的控制台应用程序。我正在努力让歌曲类首先起步,但在尝试将已分配给歌曲持续时间的 nsnumber 记录到 nslog 语句中时遇到了障碍:

//
//  Song.h
//  MusicCollection.15.9   
//
//  Created by Nicholas Iannone on 1/11/10.
   //  Copyright 2010 __MyCompanyName__. All rights reserved.
   //

   #import <Foundation/Foundation.h>


@interface Song : NSObject {

NSString *songTitle;
NSString *songArtist;
NSString *songAlbum;
NSNumber *SongDuration; 
}
@property (nonatomic, retain) NSString *songTitle, *songArtist, *songAlbum;
@property (nonatomic, retain) NSNumber *SongDuration;

-(id) init;


-(void) printSong;



@end


//
//  Song.m
//  MusicCollection.15.9    
//
//  Created by Nicholas Iannone on 1/11/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "Song.h"


@implementation Song

@synthesize  songTitle, songArtist, songAlbum;
@synthesize SongDuration;

-(id) init
{

if (self = [super init]) {

    [SongDuration numberWithInteger];
}

-(void) printSong
{



NSLog(@"===============Song Info==================");
NSLog (@"|                                       |");
NSLog (@"| %-31s |", [songTitle UTF8String]);
NSLog (@"| %-31s |", [songArtist UTF8String]);
NSLog (@"| %-31s |", [songAlbum UTF8String]);                                       
NSLog (@"| %31@   |"  [self songDuration]);
NSLog (@"|                                       |");
NSLog (@"|                                       |");
NSLog (@"=========================================");

}
@end

基本上我不知道如何将 nsnumber 合并到调用 print 方法时的 nslog 语句,加上我不太确定如何处理这些 nsobject,一般来说,它们似乎介于我要创建的对象和 ac 类型之间。任何有关如何处理这些问题的澄清将不胜感激。

谢谢,

尼克

I'm working on a console app that is tracks different songs. I'm working on getting the song class up off the ground first and have run into a snag trying to log an nsnumber which has been allocated for the song duration into an nslog statement:

//
//  Song.h
//  MusicCollection.15.9   
//
//  Created by Nicholas Iannone on 1/11/10.
   //  Copyright 2010 __MyCompanyName__. All rights reserved.
   //

   #import <Foundation/Foundation.h>


@interface Song : NSObject {

NSString *songTitle;
NSString *songArtist;
NSString *songAlbum;
NSNumber *SongDuration; 
}
@property (nonatomic, retain) NSString *songTitle, *songArtist, *songAlbum;
@property (nonatomic, retain) NSNumber *SongDuration;

-(id) init;


-(void) printSong;



@end


//
//  Song.m
//  MusicCollection.15.9    
//
//  Created by Nicholas Iannone on 1/11/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "Song.h"


@implementation Song

@synthesize  songTitle, songArtist, songAlbum;
@synthesize SongDuration;

-(id) init
{

if (self = [super init]) {

    [SongDuration numberWithInteger];
}

-(void) printSong
{



NSLog(@"===============Song Info==================");
NSLog (@"|                                       |");
NSLog (@"| %-31s |", [songTitle UTF8String]);
NSLog (@"| %-31s |", [songArtist UTF8String]);
NSLog (@"| %-31s |", [songAlbum UTF8String]);                                       
NSLog (@"| %31@   |"  [self songDuration]);
NSLog (@"|                                       |");
NSLog (@"|                                       |");
NSLog (@"=========================================");

}
@end

Basically I'm not sure how to incorporate the nsnumber into the nslog statement when the print method gets called, plus im not really sure how to deal with these nsobjects ingeneral they seem kind of in-between an object I would create and a c type. Any clarification on how to handle these would be appreciated.

Thanks,

Nick

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

简美 2024-08-24 16:17:53

要在格式字符串中插入对象的描述,请使用 %@

您也可以对标题/艺术家/专辑 NSString 执行此操作,因此无需先对它们调用 -UTF8String

对于您的歌曲持续时间,您可以直接记录 NSNumber 或通过调用 -floatValue-integerValue 并使用 %f< 记录浮点或整数表示形式/code> 和 %d

示例:

NSLog(@"%@", songTitle);
NSLog(@"%@", songDuration);
NSLog(@"%f", [songDuration floatValue]);
NSLog(@"%d", [songDuration integerValue]);

To insert an object's description in a format string, use %@.

You can do this with your title/artist/album NSStrings as well so you don't need to call -UTF8String on them first.

For your song duration, you can either log the NSNumber directly or log a float or integer representation by calling -floatValue or -integerValue and logging those with %f and %d.

Examples:

NSLog(@"%@", songTitle);
NSLog(@"%@", songDuration);
NSLog(@"%f", [songDuration floatValue]);
NSLog(@"%d", [songDuration integerValue]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文