我正在开发一个跟踪不同歌曲的控制台应用程序.我正在努力让歌曲课程首先开始,并试图将已经为歌曲持续时间分配的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 …Run Code Online (Sandbox Code Playgroud) 我接受NSString为
NSString *value = [valuelist objectAtIndex:valuerow];
NSString *value2 = [valuelist2 objectAtIndex:valuerow2];
Run Code Online (Sandbox Code Playgroud)
来自UIPickerView.我想要
double *cal = value + (value2 * 8) + 3;
NSString *message =[[NSString alloc] initWithFormat:@"%@",cal];
Run Code Online (Sandbox Code Playgroud)
在我对它进行计算之后,我应该能够在消息中获取字符串..请帮助我的程序崩溃
我试图绕过一个日期验证,拒绝接受明天之前的任何事情.
到目前为止我有这个:
NSDate *dateY = [NSDate dateWithTimeIntervalSinceNow:-86400];
// Negative one day, in seconds (-60*60*24)
NSLog(@"%@", [NSDate date]);
// "yyyy-MM-dd HH:mm:ss Z", accurate assuming Z = +0000
NSLog(@"%@", dateY);
// "yyyy-MM-dd HH:mm:ss Z", same accuracy (minus one day)
Run Code Online (Sandbox Code Playgroud)
这很好,但dateY不是NSNumber.我需要一个NSNumber比较,但我找不到任何有用的东西.(我甚至不知道怎么的NSNumber可以2011-04-14 13:22:29 +0000,反正...)
我可以NSDateFormatter用来将一个转换NSDate为一个NSString,所以如果有可能取出那个字符串并将其转换为所需的NSNumber(而不是直接转换NSDate为一个NSNumber,我似乎无法找到帮助),那就是没事的.
- (BOOL)validateDueDate:(id *)ioValue error:(NSError **)outError {
NSDate *dateY = [NSDate dateWithTimeIntervalSinceNow:-86400];
NSNumber *tis1970 = [NSNumber numberWithDouble:[dateY timeIntervalSince1970]]; …Run Code Online (Sandbox Code Playgroud) 每个值都typedef enum被视为一个int?
例如,给出以下内容typedef enum:
// UIView.h
typedef enum {
UIViewAnimationCurveEaseInOut,
UIViewAnimationCurveEaseIn,
UIViewAnimationCurveEaseOut,
UIViewAnimationCurveLinear
} UIViewAnimationCurve;
Run Code Online (Sandbox Code Playgroud)
我如何知道使用哪种方法创建NSNumber?
+ (NSNumber *)numberWithShort:(short)value;
+ (NSNumber *)numberWithUnsignedShort:(unsigned short)value;
+ (NSNumber *)numberWithInt:(int)value;
+ (NSNumber *)numberWithUnsignedInt:(unsigned int)value;
+ (NSNumber *)numberWithLong:(long)value;
+ (NSNumber *)numberWithUnsignedLong:(unsigned long)value;
+ (NSNumber *)numberWithLongLong:(long long)value;
+ (NSNumber *)numberWithUnsignedLongLong:(unsigned long long)value;
+ (NSNumber *)numberWithInteger:(NSInteger)value NS_AVAILABLE(10_5, 2_0);
+ (NSNumber *)numberWithUnsignedInteger:(NSUInteger)value NS_AVAILABLE(10_5, 2_0);
Run Code Online (Sandbox Code Playgroud)
我认为这+[NSNumber numberWithInt:]是正确的使用方法,因为使用Core Data实现Enums的最佳方法的可接受答案.例如:
[NSNumber numberWithInt:UIViewAnimationCurveLinear]
Run Code Online (Sandbox Code Playgroud)
但是,如果+[NSNumber numberWithInt:]是正确的,为什么呢?
对于按位 …
在下面的代码我得到错误消息:ARC不允许将'int'隐式转换为'NSNumber*'.
我错了什么?
<pre>
<code>
NSDictionary *results = [jsonstring JSONValue];
NSNumber *success = [results objectForKey:@"success"]; // possible values for "success": 0 or 1
if (success == 1) { // ERROR implicit conversion of int to NSNumber disallowed with ARC
}
</code>
</pre>
Run Code Online (Sandbox Code Playgroud)
感谢您的帮助或提示!
亲爱的,丹尼尔
我有一个UInt16变量,我想传递给需要NSNumber的遗留函数.
如果我尝试:
var castAsNSNumber : NSNumber = myUInt16
Run Code Online (Sandbox Code Playgroud)
我收到编译器错误 'UInt16' is not convertible to 'NSNumber'
我怎样才能将其改为NSNumber?
包含Bool的NSNumber很容易与可以包装在NSNumber类中的其他类型混淆:
NSNumber(bool:true).boolValue // true
NSNumber(integer: 1).boolValue // true
NSNumber(integer: 1) as? Bool // true
NSNumber(bool:true) as? Int // 1
NSNumber(bool:true).isEqualToNumber(1) // true
NSNumber(integer: 1).isEqualToNumber(true) // true
Run Code Online (Sandbox Code Playgroud)
但是,保留了有关其原始类型的信息,我们可以在此处看到:
NSNumber(bool:true).objCType.memory == 99 // true
NSNumber(bool:true).dynamicType.className() == "__NSCFBoolean" // true
NSNumber(bool:true).isEqualToValue(true) || NSNumber(bool:true).isEqualToValue(false) //true
Run Code Online (Sandbox Code Playgroud)
问题是:这些方法中的哪一种是确定Bool何时被包裹在NSNumber中而不是其他内容的最佳(和/或最安全)方法?都一样有效吗?或者,还有另一种更好的解决方案吗?
NSDictionary *regionDict = (NSDictionary *) region;
NSNumber *lat = [regionDict objectForKey:@"lat"];
//[latitude addObject:lat];
NSNumber *lng = [regionDict objectForKey:@"lng"];
//[longitude addObject:lng];
NSLog(@"%@%@%@",lat,lng,titles);
if(lat == [NSNumber numberWithFloat:0.00] && lng == [NSNumber numberWithFloat:0.00])
{
NSLog(@"%@%@%@",lat,lng,titles);
}
else
{
CLLocationCoordinate2D coord;
coord.latitude =lat.doubleValue;
coord.longitude =lng.doubleValue;
MapViewAnnotation *annotation =[[MapViewAnnotation alloc]initWithTitle:titles AndCoordinate:coord];
[self.mapView addAnnotation:annotation];
}
Run Code Online (Sandbox Code Playgroud)
如果由于NSNumber而不满足条件,则不检查空值.我可以检查的方式是什么?告诉我可能的方法..检查null.
以下是我的反应组件的一部分.我有一个名为daysUntil的道具进入这个包含数字的组件.在这个例子中,它正在传递数字0,这导致fontWeight函数返回700
render: function() {
return (
<Text style={this.style()}>
{this.props.day}
</Text>
)
},
style: function() {
return {
fontWeight: this.fontWeight()
}
},
fontWeight: function() {
var weight = 7 - this.props.daysUntil;
return weight * 100;
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
NSNumber类型的JSON值'700'无法转换为NSSTring.
我假设这是因为font-weight期望值为字符串格式.对此有什么正确的解决方法?
先感谢您!
根据Swift 3文档,NSNumber被桥接到Swift本机类型,如Int,Float,Double,......但是当我尝试在Dictionary中使用本机类型时,我得到了使用NSNumber修复的编译错误,为什么会这样?这是我的代码:
var dictionary:[String : AnyObject] = [:]
dictionary["key"] = Float(1000)
Run Code Online (Sandbox Code Playgroud)
并且编译器给出错误"无法将类型Float的值赋给AnyObject".如果我按如下方式编写代码,则没有问题,因为NSNumber实际上是一个对象类型.
dictionary["key"] = NSNumber(value:Float(1000))
Run Code Online (Sandbox Code Playgroud)
Swift编译器还提示将代码更正为
dictionary["key"] = Float(1000) as AnyObject
Run Code Online (Sandbox Code Playgroud)
但我不确定这是否正确.如果确实在NSNumber和本机类型(Int,Float等)之间存在桥接,为什么编译器强制转换为AnyObject?