objective-c 尝试在实例方法中 NSLog 一个 NSNumber ivar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2045691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:44:27  来源:igfitidea点击:

Trying to NSLog an NSNumber ivar in an instance method

objective-cnsnumbernslog

提问by nickthedude

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:

我正在开发一个跟踪不同歌曲的控制台应用程序。我正在努力让歌曲类首先开始,并遇到了一个障碍,试图将已分配给歌曲持续时间的 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

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.

基本上,我不确定如何在调用 print 方法时将 nsnumber 合并到 nslog 语句中,而且我不太确定如何处理这些 nsobjects 通常它们似乎介于我将创建的对象和 ac 类型之间。任何有关如何处理这些问题的说明将不胜感激。

Thanks,

谢谢,

Nick

缺口

回答by Martin Gordon

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 -UTF8Stringon them first.

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

For your song duration, you can either log the NSNumber directly or log a float or integer representation by calling -floatValueor -integerValueand logging those with %fand %d.

为了您的歌曲时长,您可以直接登录了的NSNumber或拨打登录一个浮动或整数表示-floatValue-integerValue与记录的%f%d

Examples:

例子:

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