如何将所有这些添加到NSLog中

时间:2015-04-28 00:17:10

标签: ios objective-c xcode

我有一切工作但我怎么能在一行中完成所有工作。有占位符吗?

NSLog(@“myFraction的值是:”[myFraction print],[myFraction divide]);

if (atomic_var.load(std::memory_order_acquire) == minimum) {
    std::unique_lock<std::mutex> lk(mutex);
    if (atomic_var.load(std::memory_order_acquire) == minimum) {
        //we have hit the minimum so we have to wait for the other thread to increase the variable
        condition_var.wait(lk, [&]() {
            return atomic_var.load(std::memory_order_relaxed) > minimum;
        });
    }

    //do stuff

    std::atomic_fetch_sub_explicit(&atomic_var, 1u, std::memory_order_release);
    lk.unlock();
    condition_var.notify_all();
    return;
}

//do stuff

if (std::atomic_fetch_sub_explicit(&atomic_var, 1u, std::memory_order_release) == maximum) {
    //we have hit the maximum so the other thread might be waiting
    std::atomic_thread_fence(std::memory_order_acquire);
    condition_var.notify_all();
}
//adding condition_var.notify_all() here fixes the problem but I'm afraid
//that causes a bit too great performance penalty when it could be easily avoided

打印我设置它显示分子和分母(5/20)并除以它除以numberator / denominator并且具有总计的NSLog。

3 个答案:

答案 0 :(得分:1)

您需要使用相应的String Format Specifier(访问该页面以根据您的需要找到正确的页面)。例如,将这两个值打印为浮点数:

NSLog(@"The value of myFraction is %f printed, and %f divided.", [myFraction print], [myFraction divide]);

将打印(例如):

myFraction的值为3.45,打印为1.847。

一些快速参考资料为%@ 表示字符串,%d 表示双精度数,%i 表示整数。

答案 1 :(得分:0)

以下是具有简单接口的分数类的示例。

@implementation MyFraction

- (instancetype)initWithNumerator:(NSInteger)numerator denominator:(NSInteger)denominator
{
    self = [super init];
    if (self) {
        self.numerator = numerator;
        self.denominator = denominator;
    }
    return self;
}
- (NSString *)description
{
    return [NSString stringWithFormat:@"The value of myFraction is: %ld / %ld = %f", (long)self.numerator, (long)self.denominator, [self answer]];
}
- (double)answer;
{
    if(self.denominator == 0) {
        NSLog(@"denominator must not be 0 value.");
        return NAN;
    }
    else {
        return self.numerator / (double)self.denominator;
    }
}

@end

用法:

MyFraction *f = [[MyFraction alloc] initWithNumerator:5 denominator:20];
NSLog(@"%@", f);

结果:

The value of myFraction is: 5 / 20 = 0.250000

答案 2 :(得分:-1)

NSLog(@"The value of myFraction is %f printed, and %f divided.", [myFraction print], [myFraction divide]);