我试图在 try{} catch{} 方法之后访问一个变量(特别是一个 ArrayList)。
try
{
//Here I would import data from an ArrayList if it was already created.
}
catch
{
//Create new array list if it couldn't find one.
ArrayList items = new ArrayList();
}
Run Code Online (Sandbox Code Playgroud)
以一种或另一种方式,将创建 ArrayList 项目,我希望能够访问它。我之前尝试初始化 ArrayList ,如下所示:
ArrayList items;
try
{
//Here I would import data from an ArrayList if it was already created.
}
catch
{
//Create new array list if it couldn't find one.
ArrayList items = new ArrayList();
}
Run Code Online (Sandbox Code Playgroud)
但是我无法在 try{} …
我有一个接收图像和字符串的函数。当我尝试使用 () 功能将字符串放入更长的字符串时,它告诉我在解开可选项时发现 nil。例外它根本不是可选的,它是一个字符串。我可以打印出该值并正确显示。
func UpdateBusiness(logo: UIImage, category: String) {
guard let bizID = UserDefaults.standard.string(forKey: defaultKeys.businessID) else {return}
let thisURL = "http://mywebsite.com/api/v0.1/Business/EditBusinessLogoAndCategory?businessID=\(bizID)&category=\(category)"
let combinedURL = URL(string: thisURL)!
}
Run Code Online (Sandbox Code Playgroud)
创建 URL 会使系统崩溃。我可以在调试器中看到 category 的值,并且我在这个字符串中没有可选项。它怎么能找到零?