我正在制作一个React Native函数来拉取网页的HTML.如果URL存在并且我收到200状态代码,它可以正常工作.但是,当我在其中放入一个错误的URL(会收到404错误的内容)时,会显示一个红色屏幕,显示"网络请求失败".我想在没有整个应用暂停的情况下捕获错误并向用户显示警告.我怎么能这样做呢?
fetchEvents() {
fetch('http://www.wrongurl.com', {
method: 'GET',
redirect: 'follow'
})
.then(function(response) {
if (response.status == 200) {
let responseText = JSON.stringify(response.text());
console.log(responseText);
}
else throw new Error('HTTP response status not code 200 as expected.');
})
.catch(function(error) {
console.error(error);
return error;
});
}
Run Code Online (Sandbox Code Playgroud) 我已经构建了一个带有3个选项卡的简单标签栏.
我想运行UI测试以确保如果用户单击选项卡栏项,则显示正确的视图控制器.我该怎么做呢?下面是我要开始的代码,只是不知道如何编写我的断言.
func testTabBarMyProfileButton() {
let tabBarsQuery = XCUIApplication().tabBars
tabBarsQuery.buttons["My Profile"].tap()
}
func testTabBarGraphsButton() {
let tabBarsQuery = XCUIApplication().tabBars
tabBarsQuery.buttons["Graphs"].tap()
}
func testTabBarAboutButton() {
let tabBarsQuery = XCUIApplication().tabBars
tabBarsQuery.buttons["About"].tap()
}
Run Code Online (Sandbox Code Playgroud) 在反应导航中,知道有一种方法可以在按下后退按钮时执行自定义操作。当用户滑动回到上一屏幕时,是否有类似的方法来执行此操作?
我试图从一个视图控制器转到另一个视图控制器,并将数据传递给下一个控制器。但我不断收到此错误:
Could not cast value of type 'UITableViewController' to 'UINavigationController'
Run Code Online (Sandbox Code Playgroud)
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "segueLogWaterData") {
// pass data to next view
let nav = segue.destinationViewController as! UINavigationController
let segueViewController = nav.topViewController as! WaterDataViewController
}
}
Run Code Online (Sandbox Code Playgroud)
这发生在WaterDataViewController
作为UITableViewController
. 我也试过嵌入WaterDataViewController
到 aUINavigationController
但我得到了类似的错误:
Could not cast value of type 'UITableViewController' to 'MyApp.WaterDataViewController'
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我正在尝试让NSUserDefaults在我的应用程序中运行.下面的代码应该检查NSUserDefaults中是否存在名为"iCloudOn"的bool值.如果有,则将UISwitch的值分配给NSUserDefault.如果没有,则继续向NSUserDefault分配false.
我已经标记了我收到错误的行.我收到的错误是"条件绑定中的绑定值必须是可选类型." 我无法弄清楚为什么我会收到此错误以及我需要做些什么来使这项工作.任何人都可以帮忙解决一些问题
class SettingsTableViewController: UITableViewController{
@IBOutlet weak var iCloudUISwitch: UISwitch!
let appSettings = NSUserDefaults.standardUserDefaults()
override func viewDidLoad() {
super.viewDidLoad()
//THIS IS THE LINE I AM GETTING AN ERROR ON
if let iCloudOn = appSettings.boolForKey("iCloudOn") {
//iCloud is on
iCloudUISwitch.on = appSettings.boolForKey("iCloudOn")
}
else {
//Nothing stored in NSUserDefaults yet. Set a value.
appSettings.setValue(false, forKey: "iCloudOn")
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试在基本购物列表应用程序中构建UI测试,确保滑动删除表视图单元格确实从表视图中删除单元格.
我正在运行下面的测试代码,但是当需要向左滑动表格视图单元格(以显示删除按钮)时,它不会滑动.它似乎可能正在点击它,但它不会滑动.因此,尝试点击"删除"按钮时测试失败,因为"找不到匹配按钮".
如何在表格视图中滑动删除以进行删除?
func testDeletingCell() {
let app = XCUIApplication()
app.navigationBars["ShoppingList.ShoppingListView"].buttons["Add"].tap()
let element = app.otherElements.containing(.navigationBar, identifier:"ShoppingList.AddShoppingListItemView").children(matching: .other).element.children(matching: .other).element.children(matching: .other).element
let textField = element.children(matching: .textField).element(boundBy: 0)
textField.tap()
textField.typeText("abc")
let textField2 = element.children(matching: .textField).element(boundBy: 1)
textField2.tap()
textField2.typeText("123")
app.navigationBars["ShoppingList.AddShoppingListItemView"].buttons["Save"].tap()
let tablesQuery = app.tables
tablesQuery.children(matching: .cell).element(boundBy: 0).staticTexts["123"].swipeLeft()
tablesQuery.buttons["Delete"].tap()
XCTAssert(app.tables.cells.count == 0)
}
Run Code Online (Sandbox Code Playgroud)