Click here to see the screenshot of the code.
我正在制作一个列表应用程序,我正在为我再次打开应用程序时要加载的“列表”保存数组。 奇怪的是我在模拟器上工作但不在设备上工作。 (参见截图了解)
In [1]: list1 = ["A", "B" , "C"]
In [2]: list2 = ["E", "F", "G"]
In [3]: [a + ',' + b for a, b in zip(list1, list2)]
Out[3]: ['A,E', 'B,F', 'C,G']
就是它不断崩溃的那条线。
我试图更改它,因此它是一个可选值。但我找不到任何似乎有帮助的东西..
答案 0 :(得分:1)
首先检查您的设备iOS版本是否为iOS 8或更高版本。如果小于该值,则在将值设置为NSUserDefaults后也使用synchronize()。
你强行展开(使用!)你从Down Casting获得的可选值(使用as?)。
您还可以通过添加从NSUserDefaults获取的值的检查而不是nil来解决此崩溃,然后将其分配给您的feedCells var。
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>NG-Academy ::: AngularFire Products</title>
<link rel="stylesheet" href="css/bootstrap.css">
<script src="js/angular.js"></script>
<!-- Firebase -->
<script src="https://cdn.firebase.com/js/client/2.2.4/firebase.js"></script>
<!-- AngularFire -->
<script src="https://cdn.firebase.com/libs/angularfire/1.2.0/angularfire.min.js"></script>
<script src="js/app.js"></script>
</head>
<body ng-app="myApp">
<div class="panel panel-primary" ng-controller="ProductsCtrl">
<div class="panel-heading" style="font-size:large">
Product Listttt
</div>
<form ng-submit="addFormSubmit()" ng-show="addFormShow">
<input type="text" ng-model="productName" placeholder="Name" />
<input type="text" ng-model="productCode" placeholder="Code" />
<input type="text" ng-model="description" placeholder="Description" />
<input type="text" ng-model="price" placeholder="Price" />
<input type="submit" value="Add Product" class="btn btn-default btn-success" />
</form>
<form ng-submit="editFormSubmit()" ng-show="editFormShow">
<input type="text" ng-model="productName" placeholder="Name" />
<input type="text" ng-model="productCode" placeholder="Code" />
<input type="text" ng-model="description" placeholder="Description" />
<input type="text" ng-model="price" placeholder="Price" />
<input type="submit" value="Edit Product" class="btn btn-default btn-warning" />
</form>
<div class="panel panel-body">
<table class="table">
<thead>
<tr>
<td>Product</td>
<td>Code</td>
<td>Description</td>
<td>Price</td>
<td>Actions</td>
</tr>
</thead>
<tbody>
<tr ng-repeat="product in products">
<td>{{ product.productName}}</td>
<td>{{ product.productCode }}</td>
<td>{{ product.description }}</td>
<td>{{ product.price | currency }}</td>
<td>
<button class="btn btn-primary" ng-click="showProduct(product)" >Edit</button>
<button class="btn btn-danger" ng-click="deleteProduct(product)" >Delete</button>
</td>
</tr>
</tbody>
</table>
<button class="btn btn-success" ng-click="showForm()" ng-hide="addFormShow">+</button>
<button class="btn btn-warning" ng-click="hideForm()" ng-show="addFormShow">-</button>
</div>
{{products}}
</div>
<!-- <script src="https://www.gstatic.com/firebasejs/live/3.0/firebase.js"></script> -->
<script>
// Initialize Firebase
//REMOVED for security purposes
//I have this on my version
</script>
</body>
</html>
或
将feedCells var设为可选项并删除“!”从收到错误的行的末尾开始。
let cells = NSUserDefaults.standardUserDefaults().objectForKey("feedCellsData") as? [String]
if cells != nil {
feedCells = cells
}