nic*_*ude 12 objective-c nslog nsnumber
我正在开发一个跟踪不同歌曲的控制台应用程序.我正在努力让歌曲课程首先开始,并试图将已经为歌曲持续时间分配的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
Run Code Online (Sandbox Code Playgroud)
基本上我不确定如何在调用print方法时将nsnumber合并到nslog语句中,而且我不确定如何处理这些nsobjects ingeneral它们似乎介于我要创建的对象和ac类型之间.任何关于如何处理这些的澄清将不胜感激.
谢谢,
缺口
Mar*_*don 34
要在格式字符串中插入对象的描述,请使用%@
.
您可以使用标题/艺术家/专辑NSStrings来完成此操作,因此您无需先调用-UTF8String
它们.
为了您的歌曲时长,您可以直接登录了的NSNumber或拨打登录一个浮动或整数表示-floatValue
或-integerValue
与记录的%f
和%d
.
例子:
NSLog(@"%@", songTitle);
NSLog(@"%@", songDuration);
NSLog(@"%f", [songDuration floatValue]);
NSLog(@"%d", [songDuration integerValue]);
Run Code Online (Sandbox Code Playgroud)