class Media {
var name :String = ""
init(name:String) {
self.name = name
}
}
class Song:Media {}
class Movie:Media{}
let s1 = Song(name :"Fireproof")
var m1 :Media = s1 //upcasting
//var s2 :Song = m1
var s2:Song = m1 as Song //down casting
// var x1 :Movie = m1 as Movie //
Run Code Online (Sandbox Code Playgroud)
在行上var m1: Media = s1你可以设置m1等于s1因为m1's类型是超类s1?
在线var s2: Song = m1 as Song,它被认为是"向下铸造",是因为m1: Media并且你"铸造"它"作为"一种 …
我不明白 Xcode 如何知道哪些标头应该包含在哪个目标中?例如,如果我向 Xcode 项目添加一个新文件,它会将 .m 文件添加到所选目标的编译源中,但 .h 文件呢?我的目标如何知道应该使用哪些头文件?
编辑:
我正在学习Raywenderlich的教程.我的问题是为什么我们使用可选的绑定,即if let它有什么区别?为什么我们不能使用可选链接 - 类似于A线和B线?
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("IconCell", forIndexPath: indexPath)
let icon = icons[indexPath.row]
cell.textLabel?.text = icon.title // Line A
cell.detailTextLabel?.text = icon.subtitle // Line B
if let imageView = cell.imageView, iconImage = icon.image { //Line C
imageView.image = iconImage
}
Run Code Online (Sandbox Code Playgroud)
教练的解释是:
当我们实例化图标时,我们会根据字符串获得对图像的引用...如果由于某种原因图像未包含在包中,重命名或删除,则该图标可能没有与之关联的图像.我们必须使用,如果让它确保它在那里.
我仍然不明白其中的区别.
我们的 iPhone 应用程序目前支持 IOS 8/9/10。我很难支持自定义 UITableViewCell 的可访问性语音。我已经浏览了以下 SO 帖子,但没有一个建议奏效。我希望可以访问各个组件。
对我来说不幸的是,可访问性检查器未检测到该单元格。有没有办法通过可访问性来选择表格视图单元格中的单个元素?在设备和模拟器上调试此问题时,我发现 XCode 调用isAccessibleElement函数。当函数返回时NO,将跳过其余的方法。我正在 XCode 中测试 IOS 9.3。
我的自定义表格视图单元格由一个标签和一个开关组成,如下所示。
标签被添加到内容视图,而开关被添加到自定义附件视图。
接口定义如下
@interface MyCustomTableViewCell : UITableViewCell
///Designated initializer
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier;
///Property that determines if the switch displayed in the cell is ON or OFF.
@property (nonatomic, assign) BOOL switchIsOn;
///The label to be displayed for the alert
@property (nonatomic, strong) UILabel *alertLabel;
@property (nonatomic, strong) UISwitch …Run Code Online (Sandbox Code Playgroud) 我用这段代码看到了这个问题:
protocol Flashable {}
extension Flashable where Self: UIView
{
func flash() {
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseIn, animations: {
self.alpha = 1.0 //Object fades in
}) { (animationComplete) in
if animationComplete == true {
UIView.animate(withDuration: 0.3, delay: 2.0, options: .curveEaseOut, animations: {
self.alpha = 0.0 //Object fades out
}, completion: nil)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
而且我想知道为什么我们不只是直接扩展UIView?或者在类似的情况下延伸UIViewController为什么用a扭转它where Self:
每当应用程序首次运行时,是否会调用位置管理器(_:执行更改授权:),即使已经调用了位置管理器方法requestWhenInUseAuthorization()或startUpdatingLocation()?我正试图通过点击我在下面的@IBAction中调用的按钮来报告位置:
@IBAction func findOutPressed(_ sender: UIButton) {
getLocation()
}
Run Code Online (Sandbox Code Playgroud)
我的CoreLocation代码位于以下扩展中:
extension ViewController: CLLocationManagerDelegate {
// Called from findOutPressed to get location when button is clicked
func getLocation() {
let status = CLLocationManager.authorizationStatus()
handleLocationAuthorizationStatus(status: status)
}
// Respond to the result of the location manager authorization status
func handleLocationAuthorizationStatus(status: CLAuthorizationStatus) {
switch status {
case .notDetermined:
locationManager.requestWhenInUseAuthorization()
case .authorizedWhenInUse, .authorizedAlways:
locationManager.startUpdatingLocation()
case .denied:
print("I'm sorry - I can't show location. User has not authorized it")
case .restricted:
print("Access denied - likely …Run Code Online (Sandbox Code Playgroud) core-location uiapplicationdelegate cllocationmanager ios swift
我试图保护从字符串到Float到Int的转换:
guard let v = Int (Float("x")) else {
return -1
}
Run Code Online (Sandbox Code Playgroud)
swift 3编译器抱怨:
可选类型'Float?'的值 没有打开; 你的意思是用'!' 要么 '?'?
添加"?" 但是没有帮助.并且"!" 这里会错的,不是吗?
是否有可能解决这个问题,而不必使用两行或两个保护声明?
我知道所有 UI 更新都必须从主线程完成。
但纯粹是为了更深入地了解 GCD 和 dispatch main 是如何工作的:
我有一个运行网络调用的按钮,我最终在它的 completionHandler 中执行以下操作:
self.layer.borderColor = UIColor(red: 255/255.0, green: 59/255.0, blue: 48/255.0, alpha: 1.0).cgColor
self.layer.borderWidth = 3.0
Run Code Online (Sandbox Code Playgroud)
发生颜色变化需要 6-7 秒。显然,如果从主线程运行上面的代码,它会立即改变边框颜色。
问题 1即使我没有任何其他代码可以运行,为什么 UI 更改不会立即从后台线程发生?还在等什么?
有趣的是,如果我点击按钮进行网络调用,然后点击textField 本身(在 6-7 秒之前),边框颜色会立即改变。
发生这种情况是因为:
从后台线程我已经更新了模型,即更改将要更新的 UI/视图排队的 textField 颜色……但是由于我们在后台队列中,因此更新 UI 可能需要几秒钟的时间
但随后我立即点击了 textField 并强制超级快速读取 textField 及其所有属性,包括边框 - 从主线程(实际用户触摸始终通过主线程处理)......即使还没有屏幕上显示为红色,但由于模型上显示为红色,因此它将从中读取并立即将颜色更改为红色。
问题 2:这种观察是否正确?
如果我不点击并等待:
如果我点击:
我的完整代码如下:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var textField: UITextField!
@IBAction func isValid(_ sender: Any) {
let userEmail = textField.text …Run Code Online (Sandbox Code Playgroud) 我问这个问题的原因是因为有时我运行该项目,并等待 2-3 分钟,期望它能够运行,但当我离开屏幕并浪费时间时它会失败。
编写一段只能执行一次的代码的最简单方法是什么?
我知道一种方法但有问题.
首先,我写了一个布尔变量,它具有负值,但可以设置为正值,之后不能更改
var hasTheFunctionCalled : Bool = false {
didSet{
hasTheFunctionCalled = true
}
}
Run Code Online (Sandbox Code Playgroud)
然后在其中编写函数和代码:
func theFunction(){
if !hasTheFunctionCalled{
//do the thing
}
hasTheFunctionCalled = true
}
Run Code Online (Sandbox Code Playgroud)
但问题是变量可以从范围内的其他地方更改,这个解决方案看起来并不那么简单和具体.
swift ×7
ios ×3
optional ×2
xcode ×2
casting ×1
compilation ×1
downcast ×1
function ×1
header-files ×1
lazy-loading ×1
objective-c ×1
protocols ×1
static ×1
uitableview ×1
upcasting ×1
voiceover ×1