如何使用网页中的数据填充我的Swift应用程序?
我想创建一个应用程序,该应用程序组织和显示我想要的数据为我的狂热Wiki,因此我在iPad Pro上下载了Swift Playgrounds,完成了所有教程,现在正在研究它。我已经非常熟悉C/C ++/C#,Java,Python和其他几种语言(不是Objective-C),但是Swift教程仅涵盖了基础知识,View
s,并且并没有真正进入独特的Swift编码。尽管我可以手动输入所有数据并使应用程序更快地工作速度,但这将是一个巨大的痛苦,并且Wiki上的数据随着游戏的更新而可能会更改。
我已经搜索了在这里和一般网络搜索其他几个网站的搜索方式,但所描述的一种方法没有一种可用的方法,并为我提供了可用的东西。一些旧的Objective-C指南指示对String
的生成表(数组阵列)与WebPage表中的数据的生成表(数组阵列)的基本功能,但我在迅速等效上找不到任何东西。因此,我假设我必须编写自己的分类功能。只要我可以从网页上获得最新的源代码的字符串,但这就是我似乎无法获得的部分,我就不会遇到麻烦。
在我看到并尝试的所有方法中(所有失败),我喜欢这是最好的:
var source = String(contentsOf: URL(string: String)!, encoding: .ascii)
string
是引号中的拷贝性perl url。
我得到了一个错误说可以抛出的错误,并且在属性初始化器中不允许使用它,我想解决这个问题,但是即使我有:
let address = URL(string: String)
var source = String(contentsOf: address, encoding: .ascii)
它告诉我,“不能在属性初始化器中使用实例成员'地址”;属性初始化器在可用的“自我”之前运行。”如果我将字符串
将其分开到上面的 ,则在第二个Let
上出现错误。 Swift教程甚至没有解释LET
和var
的区别“ const var。”无论我使用哪种错误,都不会改变。
这为我提供了一个无用的字符串
包含“ application/octet-stream”
,但是唯一没有引起错误的人:
var resources = UTType(filenameExtension: URL(string: String)!.pathExtension)?.preferredMIMEType ?? "application/octet-stream"
我从未见过<代码> ?? 之前,我必须查找。我的理解是,这是Swift中的运营商,用于确定另一个是nil
的默认设备,但让我知道我是否错了。我也不太了解uttype或MimeType。
这些教程也没有在这些用途中解释?
和!。我本来可以猜到的指针,但是考虑到var
的使用,我听说过的谣言我认为Swift更像是C#,因为整体上缺乏指针。如果不是指针(数据与内存地址),那么我猜想根据我遇到的过去错误,就像“原始”和“稳定”表格。 ?
将其带到“原始”表单和!
将其带到“稳定”表单中。
I wanted to create an app that organizes and displays the data I want in a fandom wiki for me, so I downloaded Swift Playgrounds on an iPad Pro, finished all the tutorials, and I’m working on it now. I’m already decently familiar with C/C++/C#, Java, Python, and a couple other languages (not Objective-C), but the Swift tutorials only really covered the basics and View
s, and didn’t really get into the unique Swift coding. Although I could manually input all the data and make the app work faster and offline, that’d be a huge pain, and the data on the wiki could change as the game updates.
I’ve searched for how here and with general web searches for several other sites, but not a single one of the methods described worked and provided me with something usable. Some old Objective-C guides indicated base capabilities of sorting webpage source data and generating tables (arrays of arrays) of String
s with data from a webpage table, but I couldn’t find anything on a Swift equivalent. Thus I’m assuming I’ll have to write my own sorting functions. I wont have trouble with that so long as I can get a String with the up to date source code from the webpage, but that’s the part I can’t seem to get.
Of all the methods I saw and tried (all of which failed) I like this one the best:
var source = String(contentsOf: URL(string: String)!, encoding: .ascii)
Where String
is the copy-pasted URL in quotation marks.
I get an error saying it can throw, and that that’s not allowed in a property initializer, and I’d like a way around that, but even if I have:
let address = URL(string: String)
var source = String(contentsOf: address, encoding: .ascii)
It tells me, “Cannot use instance member ‘address’ within property initializer; property initializer run before ‘self’ is available.” If I separate the String
out to a let
above that one the error comes up on the second let
. The Swift tutorials didn’t even explain what the difference between let
and var
was, but through past error messages I’m guessing let
is a “const var.” Regardless of which I use the error doesn’t change.
This one provided me with a useless String
containing "application/octet-stream"
, but was the only one that didn’t cause an error:
var resources = UTType(filenameExtension: URL(string: String)!.pathExtension)?.preferredMIMEType ?? "application/octet-stream"
I’ve never seen ??
before so I had to look that up. My understanding is that it’s an operator in Swift used to identify a default for if the other is nil
, but let me know if I’m wrong. I don’t understand the UTType or MIMEType very well either.
The tutorials also didn’t explain the ?
and !
in these kinds of uses. I would have guessed pointers, but given the use of var
and rumors I’ve heard I think Swift is more like C# with the whole lack of pointers. If not pointers (data vs memory address) then I’m guessing there’s like a “raw” and “stable” form based on past errors I’ve gotten. Where ?
brings it down to the “raw” form and !
brings it up to the “stable” form.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我修复了代码,并同时找出了针对小烦恼的解决方案。
属性初始化器中的“不能使用实例成员'地址”;属性初始化器在“可用”之前运行”错误是通过没有
url
的单独变量来解决的。无论如何,我认为这很烦人。使用尝试?
将值转换为nil
,如果丢弃了错误,请修复属性初始化器中不允许抛出的限制。这输出了包装(我在原始帖子中称为“原始”的包装;由于尚不知道术语)的数据类型版本,因此我必须将其转换为未包装(我称之为“稳定”为了使用数据类型的原始帖子)。我认为,与包装和未包装的可选数据类型的概念相比,更好/更清洁的解决方案是设计能够处理掷球的方法,但我想发明要解决解决方案的问题就是迅速滚动。我还注意到,尽管指南指示了什么,但不需要为我需要实现的目标指定编码。我现在应该能够完成其余的内容。
I fixed the code, and figured out a solution to the minor annoyance at the same time.
The “Cannot use instance member ‘address’ within property initializer; property initializer run before ‘self’ is available” error is fixed by not having a separate variable for the
URL
; which I thought having was annoying anyway. Usingtry?
converts the value tonil
if an error is thrown, fixing the limitation of throws not being allowed in the property initializer. This outputs the wrapped (what I referred to as “raw” in the original post; due to not knowing the terminology yet) version of the datatype, so I have to convert it to the unwrapped (what I referred to as “stable” in the original post) version of the datatype in order to use it.I think a better/cleaner solution than the concept of wrapped and unwrapped optional datatypes would be designing methods to be able to handle throws, but I guess inventing problems to have solutions for is just how Swift rolls. I also noticed that despite what the guides indicated, the encoding does not need to be specified for what I need to accomplish. I should be able to finish the rest of it now.