以下是用于在playground中执行Switch语句的代码.我执行了几个switch语句而没有使用default.我怀疑的是为什么它对某些人来说是可选的而且对于其他陈述是强制性的.谢谢提前!
let someNumber = 3.5
switch someNumber {
case 2 , 3 , 5 , 7 , 11 , 13 :
print("Prime numbers")
case 4 , 6 , 24 , 12 , 66 :
print("Normal numbers")
}
Run Code Online (Sandbox Code Playgroud)
计数器语句在不使用默认值的情
let yetAnotherPoint = (3,1)
switch yetAnotherPoint {
case let (x,y) where x == y :
print("(\(x),\(y)) is on the line x == y")
case let (x,y) where x == -y :
print("(\(x),\(y)) is on the line x == -y")
case let (x,y): …Run Code Online (Sandbox Code Playgroud) 我正在使用 Xcode8 和 swift 3。所以我将我的项目完全转换为最新的语法,现在我收到错误:
在此上下文中,“错误”对于类型查找不明确
在函数中:
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
//do stuff
}
Run Code Online (Sandbox Code Playgroud)
FBSDK中的函数声明如下:
- (void) loginButton:(FBSDKLoginButton *)loginButton
didCompleteWithResult:(FBSDKLoginManagerLoginResult *)result
error:(NSError *)error;
Run Code Online (Sandbox Code Playgroud)
所以我尝试将错误类型更改为 NSError 但出现另一个错误:
“SignInViewController”不符合协议“FBSDKLoginButtonDelegate”
我不知道该怎么办。任何帮助表示赞赏!
具有以下功能.我希望在完成线程执行后将函数的结果作为Int返回.它正在查询外部设备的变量.当我调用函数get变量时,我立即收到结果-1,然后几秒后我从完成线程收到结果.如何重新处理此项,以便在返回实际值之前不返回任何结果?仍然是Swift3和GCD的菜鸟.谢谢
func getVariable(variableName: String) -> Int {
var res: Int = -1
print (deviceOK)
if deviceOK {
DispatchQueue.global(qos: .default).async {
// logging in
(self.deviceGroup).wait(timeout: DispatchTime.distantFuture)
(self.deviceGroup).enter()
self.myPhoton!.getVariable(variableName, completion: { (result:Any?, error:Error?) -> Void in
if let _ = error {
print("Failed reading variable " + variableName + " from device")
} else {
if let res = result! as? Int {
print("Variable " + variableName + " value is \(res)")
self.deviceGroup.leave()
}
}
})
}
}
return res
}
Run Code Online (Sandbox Code Playgroud) 我正试图通过该函数将数据传递给我的第二个VC上的标签func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView).
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
print("Annotation selected")
if let annotation = view.annotation as? POIAnnotations {
let destVC : ShopDetailViewController
destVC.shopName.text = annotation.title!
print("Your annotation title is: \(annotation.title!)")
}
}
Run Code Online (Sandbox Code Playgroud)
当我设置shopName.text为annotation.title,我得到一个错误说明:
在初始化之前使用常量'destVC'.
我不太清楚出了什么问题.
我正在尝试string range在字符串中设置两个不同的粗体字体.我知道如何做到这一点range的range,但有可能与多个范围内做一次.我不喜欢重复代码.
这是我的代码
if let rankString = trophy.fullRank.value {
var secondRankString = "some string"
var string = SRUtils.LocalizedString("unlocked.description", comment: "")
string = String(format: string, trophy.trophiesNumber.value!, rankString)
let atStr = NSMutableAttributedString(string: string)
//Here i need to add rankString and other range
let textRange = (string as NSString).range(of: rankString)
if let font = UIFont(name: "HelveticaNeue-Bold", size: 16) {
atStr.addAttribute(NSFontAttributeName, value: font, range: textRange)
self.trophyDescription.value = atStr
}
}
Run Code Online (Sandbox Code Playgroud)
如果例如:
var string = "Hello do you need …Run Code Online (Sandbox Code Playgroud) 假设我有一个名为Number的类,它有a bool even和a bool big(忽略它们的含义).
我想构建一个函数,它接收2个数字a和b,并为bool的所有可能性做一个不同的事情:
func tooMuchCodeToWrite(a: Number, b: Number){
if(!a.even && !a.big && !b.even && !b.big){
//do something
}
else if(!a.even && !a.big && !b.even && b.big){
//do something
}
else if(!a.even && !a.big && b.even && !b.big){
//do something
}
else if(!a.even && !a.big && b.even && b.big){
//do something
}
// ...
else if(a.even && a.big && b.even && !b.big){
//do something
}
else{ //everyone is true in this case …Run Code Online (Sandbox Code Playgroud) 我尝试在 swift 3 中每秒刷新一次标签。我总是收到以下错误:
Showing Recent Issues
Command failed due to signal: Segmentation fault: 11
Run Code Online (Sandbox Code Playgroud)
这是我使用过的代码:
var TimerBeacon = Timer.scheduledTimer(timeInterval: 1, target: self,
selector: Selector(("Timer")), userInfo: nil, repeats: true)
func Timer() {
//here is the label to refresh
}
Run Code Online (Sandbox Code Playgroud)
我希望有人能帮助我:)
我用数据源数组制作了UIPageViewController,它运行正常.但是当向右滑动转发时,请调用After和Before方法.
我的代码在这里:
import UIKit
class MainPageViewController: UIPageViewController,UIPageViewControllerDataSource {
override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
initUPageVC(idx:0,isAnimate:false,direction: UIPageViewControllerNavigationDirection.forward)
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?
{
//PRIVIOUS
let pageContent: MainPageContentViewController = viewController as! MainPageContentViewController
var index = pageContent.index
if ((index == 0) || (index == NSNotFound))
{
return nil
}
index -= 1;
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "updateMenuBarItemSelectedNotif"), object: index)
return getViewControllerAtIndex(index)
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
{
//NEXT
let pageContent: MainPageContentViewController = viewController …Run Code Online (Sandbox Code Playgroud) 错误来自于此.在Swift 2.3下,这很好
selectedIndexPaths = selectedIndexPaths.filter() { $0 !== indexPath}
Run Code Online (Sandbox Code Playgroud)
哪里:
var selectedIndexPaths: [IndexPath] = []
Run Code Online (Sandbox Code Playgroud)
有什么建议吗?
我试图以编程方式设置从左到右对齐的搜索栏文本,但似乎它在相反的方向(如英语)添加了文本,然后搜索没有结果。感谢任何人的帮助。