在我的模型中,我想要一个外键的可选字段.我试过这个:
field = models.ForeignKey(MyModel, null=True, blank=True, default=None)
Run Code Online (Sandbox Code Playgroud)
但我得到这个错误:
model.mymodel_id may not be NULL
Run Code Online (Sandbox Code Playgroud)
我正在使用sqlite编辑:如果它可以帮助,这里是异常位置:
/usr/lib/python2.6/site-packages/django/db/backends/sqlite3/base.py in execute, line 200
Run Code Online (Sandbox Code Playgroud)
所以这是sqlite特定的问题,我想.
我的字典String是图像url的.当我尝试NSURL从这个Strings 创建对象时,编译器抛出一个错误"致命错误:在解开一个Optional值时意外地发现了nil" .我搜索了这个问题并没有找到解决方案.每个人都说"你的变量是零".但我的变量不能为零,我的代码日志也显示.
这是我的代码:
var article: [String:String!]!
...
//viewDidLoad method
let imageURLString : String = article["image"]!
println(imageURLString) // log: http://domain.com/img/path.jpg
let imgURL : NSURL = NSURL.URLWithString(imageURLString) // error here
Run Code Online (Sandbox Code Playgroud)
我收到"致命错误:在展开Optional值时意外发现nil"
问题出在哪儿?希望有人可以提供帮助.
我刚开始使用Swift.所以我创建了一个带有标签,按钮和文本字段的简单应用程序.单击该按钮时,应用程序必须使用文本字段的文本更改标签.
class ViewController: UIViewController {
@IBOutlet weak var textLabel: UILabel!
@IBOutlet weak var textField: UITextField!
@IBAction func updateButton(sender: UIButton) {
textLabel.text = "Hi \(textField.text) "
}
Run Code Online (Sandbox Code Playgroud)
结果是
嗨可选("TextOfTextField")
好的.所以这是一个非常简单的问题.
我希望有一个人可以帮助我.
如果我有一个元组列表,我可以转换为一个地图toMap:
val x = (3 -> 3)
List(x).toMap
Run Code Online (Sandbox Code Playgroud)
我明白了
scala.collection.immutable.Map[Int,Int] = Map(3 -> 3)
Run Code Online (Sandbox Code Playgroud)
如果我有一个可选列表并尝试相同,我会收到一个错误:
val x = Some(3 -> 3)
val y = None
List(x, y).toMap
<console>:15: error: Cannot prove that Some[(Int, Int)] <:< (T, U).
Run Code Online (Sandbox Code Playgroud)
我想得到相同的结果.可能吗?
我试图使用a std::optional来实例化一个对象(之前无效).我发现了一个烦人的情况,我不知道如何优雅地解决这个问题.
我有以下数据结构:
struct Foo {
int foo1;
float foo2;
};
Run Code Online (Sandbox Code Playgroud)
作为会员std::optional<Foo> foo_.
在一个功能
void Bar::bar(int const value1, float const value2) {
foo_.emplace(value1, value2);
}
Run Code Online (Sandbox Code Playgroud)
令我惊讶的是,这无法编译(在GCC 7.1中),因为它试图调用Foowith 的构造函数int const&, float const&.现在天真的我试图专注emplace于:
foo_.emplace<int, float>(value1, value2);
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为它试图使用initializer_list那么.
所以我的问题是如何优雅地召唤出来?
题:
当我可以检查值是否为零时,为什么Swift会使用类似选项的东西?从本质上讲,我想知道这种语言功能的用途是什么,我正在学习Swift atm,从初学者的角度来看,它似乎多余/无用.
PS:我来自Javascript.
码:
做一些像我这样的事情似乎很荒谬:
if let normalImage = imageFromFaceBook {
print(normalImage) // normalImage is a constant
} else {
print("There is no image")
}
Run Code Online (Sandbox Code Playgroud)
当我可以做的时候:
if (imageFromFacebook != nil) {
print(imageFromFacebook) // normalImage is a constant
} else {
print("There is no image")
}
Run Code Online (Sandbox Code Playgroud)
像我通常在Javascript中做的那样:
if (imageFromFacebook != null) {
print(imageFromFacebook) // normalImage is a constant
} else {
print("There is no image")
}
Run Code Online (Sandbox Code Playgroud) 如何使用Optional和lambda编写下面的逻辑? 我有两个列表
List success = new ArrayList <>();
List failures = new ArrayList <>();
并且有一个对象
RoomTypes值
对于这value.getErrorType()可能是空或不和value.getId()回报整数.
我想更新success,并failure与value.getId()根据列表value.getErrorType()为空.
就像是:
if(value.getErrorType().equals(NULL)){
success.add(value.getId())
}
else{
failure.add(value.getId())
}
Run Code Online (Sandbox Code Playgroud) 有没有更好的方法在JAVA 8中编码?
if (info1 != null && info1.getId() != null && info1.getPartyNumber()!= null) {
billing.setSenderId(info1.getId());
billing.setSenderNumber(info1.getPartyNumber());
}
if (info2 != null && info2.getId() != null && info2.getPartyNumber()!= null) {
billing.setReceiverId(info2.getId());
billing.setSellerNumber(info2.getPartyNumber());
}
..
..
Run Code Online (Sandbox Code Playgroud)
提前致谢.注意:我调查Optional.ofNullable()但不确定这是否真的有助于多次检查?
我正在尝试用Manning的Manning的“ Akka in Action”重写一个POC项目的scala示例。该项目是用于创建事件和购买票证的小型Http服务器。
我正在演员可以发送影片Optional<Event>给我的时刻RestApi。根据是否存在该值,我应该使用OKelse 完成调用NOT_FOUND。
在Scala中,代码段如下所示:
get {
// GET /events/:event
onSuccess(getEvent(event)) {
_.fold(complete(NotFound))(e => complete(OK, e))
}
}
Run Code Online (Sandbox Code Playgroud)
... where getEvent返回一个Option[Event](等于java的Optional<Event>)。这就是我用Java重写的方式:
get(() -> onSuccess(() -> getEvent(event), eventGetRoute()))
...
//and eventGetRoute() is a function:
private Function<Optional<Event>, Route> eventGetRoute() {
return maybeEvent -> maybeEvent.map(event -> complete(OK, event, Jackson.marshaller())).orElseGet(() -> complete(NOT_FOUND));
}
Run Code Online (Sandbox Code Playgroud)
无法编译:Bad return type in lambda expression: Route cannot be converted to RouteAdapter。较长的(第一个)complete返回a …
我有 let intervalId = option(Js.Global.intervalId)
我想以一种简洁的方式对Js.Global.clearInterval选项是否具有值进行副作用调用(即is Some(id)和not None)
也许Belt.Option.map功能就是答案,但是我在使用它时遇到了问题。
我是OCaml和ReasonML的新手,但是我知道的几种语言都有合适的功能。我在这里释义其中的一些,以给出我想要的想法:
在斯卡拉,我会说: intervalId.foreach(Js.Global.clearInterval)
在Swift中,我会说: intervalId.map(Js.Global.clearInterval)
optional ×10
swift ×3
ios ×2
java ×2
java-8 ×2
scala ×2
string ×2
akka ×1
akka-http ×1
bucklescript ×1
c++ ×1
c++17 ×1
conditional ×1
dictionary ×1
django ×1
foreign-keys ×1
if-statement ×1
lambda ×1
nsurl ×1
null ×1
reason ×1
sqlite ×1
tuples ×1