我正在尝试为我的Swift应用程序创建一个视频播放器,但我不断收到"未解析的标识符AVPlayerViewController"的错误.我错过了什么?
我是初学者,我可能要用外行的话来问几千次.我一直在网上搜索可能有一天的视频,如何将Youtube视频嵌入到我的应用程序中,没有结果.如果你能指出一个很棒的教程!
谢谢!
我正在使用"使用文本工具包管理iOS应用程序中的文本"教程.它是为Objective C编写的,但我认为无论如何我都会尝试使用Swift.
但是,当我得到以下代码时,我无法弄清楚如何为UITextView使用Swift 设置标题和其他样式.这是Objective C代码:
- (IBAction)applyHeadlineStyle:(id)sender {
[_textView setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleHeadline]];
}
- (IBAction)applySubHeadlineStyle:(id)sender {
[_textView setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline]];
}
- (IBAction)applyBodyStyle:(id)sender {
[_textView setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleBody]];
}
- (IBAction)applyFootnoteStyle:(id)sender {
[_textView setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleFootnote]];
}
- (IBAction)applyCaption1Style:(id)sender {
[_textView setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleCaption1]];
}
- (IBAction)applyCaption2Style:(id)sender {
[_textView setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleCaption2]];
}
Run Code Online (Sandbox Code Playgroud)
我试过了
textView.setFont =
textView.preferredFontForTextStyle =
Run Code Online (Sandbox Code Playgroud)
然而,这些似乎都不起作用,而且我找不到任何Swift答案.
我有一个由单词组成的字符串,其中一些包含标点符号,我想删除它,但我一直无法弄清楚如何执行此操作.
例如,如果我有类似的东西
var words = "Hello, this : is .. a string?"
Run Code Online (Sandbox Code Playgroud)
我希望能够创建一个数组
"[Hello, this, is, a, string]"
Run Code Online (Sandbox Code Playgroud)
我最初的想法是使用类似的东西words.stringByTrimmingCharactersInSet()删除我不想要的任何字符,但这只会使字符脱离结束.
我想也许我可以用一些东西来迭代字符串
for letter in words {
if NSCharacterSet.punctuationCharacterSet.characterIsMember(letter){
//remove that character from the string
}
}
Run Code Online (Sandbox Code Playgroud)
但我不确定如何从字符串中删除字符.我确信if语句的设置方式也存在一些问题,但它显示了我的思考过程.
在我的代码中,我有一个UITextField,当用户点击时打开一个UIDatePicker,使用户能够轻松有效地滚动到他们的出生日期.显然,我们不希望UIDatePicker像目前那样向上滚动到2015年及以上.因为它是出生日期输入字段,我还需要能够将条目限制为16年+.我该怎么做呢?
class SignUpViewController: UIViewController, UITextFieldDelegate {
var datePicker:UIDatePicker!
@IBOutlet weak var dateTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// UI DATE PICKER SETUP
var customView:UIView = UIView(frame: CGRectMake(0, 100, 320, 160))
customView.backgroundColor = UIColor.clearColor()
datePicker = UIDatePicker(frame: CGRectMake(0, 0, 320, 160))
datePicker.datePickerMode = UIDatePickerMode.Date
customView.addSubview(datePicker)
dateTextField.inputView = customView
var doneButton:UIButton = UIButton (frame: CGRectMake(100, 100, 100, 44))
doneButton.setTitle("Done", forState: UIControlState.Normal)
doneButton.addTarget(self, action: "datePickerSelected", forControlEvents: UIControlEvents.TouchUpInside)
doneButton.backgroundColor = UIColor .grayColor()
dateTextField.inputAccessoryView = doneButton
Run Code Online (Sandbox Code Playgroud) 我正在使用Xcode 6.4
我有一个UIViews数组,我想转换为带键的字典"v0", "v1"....像这样:
var dict = [String:UIView]()
for (index, view) in enumerate(views) {
dict["v\(index)"] = view
}
dict //=> ["v0": <view0>, "v1": <view1> ...]
Run Code Online (Sandbox Code Playgroud)
这有效,但我试图以更实用的方式做到这一点.我想我必须创建dict变量让我感到困扰.我很乐意使用enumerate()并reduce()喜欢这样:
reduce(enumerate(views), [String:UIView]()) { dict, enumeration in
dict["v\(enumeration.index)"] = enumeration.element // <- error here
return dict
}
Run Code Online (Sandbox Code Playgroud)
这感觉更好,但我得到错误:Cannot assign a value of type 'UIView' to a value of type 'UIView?'我已经尝试过其他对象UIView(即:) [String] -> [String:String],我得到了相同的错误.
有关清理的建议吗?
考虑:
enum Test {
case foo
case bar
case baz
case etc
}
var test: Test = ...
Run Code Online (Sandbox Code Playgroud)
我想测试枚举是否bar特别.我可以使用switch语句:
switch test {
case .bar:
doSomething()
default:
break
}
Run Code Online (Sandbox Code Playgroud)
如果我可以改为使用它会更加简洁if:
if test == .bar {
doSomething()
}
Run Code Online (Sandbox Code Playgroud)
但除非我遗漏了什么,否则没有办法做到这一点:
Binary operator '==' cannot be applied to two 'Test' operands
这是否可能,如果不是,这是一个刻意的语言设计决定吗?
替代标题
- 如何在iPhone上的Objective-C下载网页数据?
- 如何将html资源文件的全部内容读入
NSString *- 如何在Swift中下载网页数据?
- 将NS页保存在NSString中
如何获取网页数据并将数据放入字符串?
就像是:
NSString *webPageBody = GetWebPageData("www.mysite.com/webpage.html");
Run Code Online (Sandbox Code Playgroud)
在webPageBody,我想看到"HTML我的网页HTML".
我有一个日期,我需要拆分一些组件.例如
let components = NSCalendarUnit.CalendarUnitDay | NSCalendarUnit.CalendarUnitHour
let date = calendar.components(components, fromDate: aDate, toDate: NSDate(), options: nil)
var dateToPrint = "\(date.day) days \(date.hour) hours"
Run Code Online (Sandbox Code Playgroud)
dateToPrint将是从aDate到现在的天数和小时数.但如果我想要几周而不是几天
let components = NSCalendarUnit.CalendarUnitWeek | NSCalendarUnit.CalendarUnitHour
let date = calendar.components(components, fromDate: aDate, toDate: NSDate(), options: nil)
var dateToPrint = "\(date.week) weeks \(date.hour) hours"
Run Code Online (Sandbox Code Playgroud)
date.week不存在.那我怎么解决这个问题呢?
我还没想到或者能够在网上找到的东西.
有没有办法将其他字段添加到包含JSON数据中不存在的可解码协议的结构中?
例如,简单,假设我有一个json对象的数组结构
{"name":"name1","url":"www.google.com/randomImage"}
但是我想要将UIImage变量添加到包含可解码的结构中,例如
struct Example1: Decodable {
var name: String?
var url: String?
var urlImage: UIImage? //To add later
}
Run Code Online (Sandbox Code Playgroud)
有没有办法实现可解码协议,以便从JSON获取名称和URL,但允许我稍后添加UIImage?