我有这样的字符串
var str = "@text1 this is good @text1"
Run Code Online (Sandbox Code Playgroud)
现在换成text1另一个字符串,比方说t 1.我能够替换文本,但我无法加粗.我想加粗新字符串t 1,以便最终输出为:
@t 1这是好的@t 1
我该怎么做?
我看到的所有示例都在Objective-C中,但我想在Swift中进行.
提前致谢.
使用Swift 3.0在游乐场工作我有这个代码:
struct Test {
func run() {
var timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { timer in
print("pop")
}
}
}
let test = Test()
test.run()
Run Code Online (Sandbox Code Playgroud)
但没有什么是打印到控制台.我读过如何在Swift中使用NSTimer?我在网上的答案和教程中看到的计时器的大部分用法涉及一个选择器,所以我尝试了这个:
class Test {
func run() {
var timer = Timer.scheduledTimer(timeInterval: 0.4, target: self, selector: #selector(self.peep), userInfo: nil, repeats: false)
}
@objc func peep() {
print("peep")
}
}
let test = Test()
test.run()
Run Code Online (Sandbox Code Playgroud)
似乎仍然没有打印到控制台.如果我添加timer.fire(),那么我得到控制台打印,但显然这违背了目的.我需要更改什么才能让计时器运行?
编辑:
所以CFRunLoopRun()在我run为我的Teststruct 调用方法之后添加就可以了.非常感谢那些回答的人,特别是@AkshayYaduvanshi(他的评论指向我CFRunLoopRun())和@JoshCaswell(他的答案提出了我的计时器只适用于运行循环的事实).
我正在使用 java 程序解析来自短信服务的文本,我看到在使用 DateTimeFormatter 时,'E' 或 'e' 是一周中几天的模式字符,并且字符数决定了格式的类型(即'E' = T, 'EE' = Tu, 'EEE' = Tue, 等等...) 但在我的程序中,我永远不确定消息将包含什么格式。
如果我要检查一周中的哪一天,是否可以使用单个模式检查所有格式?如果没有,当我检查完全写出的天数(字符长度不同)时,连续有多少个 E 将同时包含星期一(6 个字母)和星期三(9 个字母)?
例如:
String singleWordFromMessage = "thursday";
LocalDateTime potentialDate = LocalDateTime.parse(singleWordFromMessage, DateTimeFormatter.ofPattern("EEEE"));
Run Code Online (Sandbox Code Playgroud)
我错过了文档中的工作方式,到目前为止我在这里的搜索没有取得成果。在此先感谢您的帮助。
我正在尝试将CKReference添加到云工具包中的记录中,但尝试会一直触发"服务记录更改".从我的println显示的控制台消息(控制台消息和代码如下),我上传了带有0个引用的记录,然后当我附加引用时,我看到尝试上传带有1个引用的记录.然后我收到了错误.
据我了解,不应触发"服务记录已更改",因为参考列表中的值已更改(记录有一个完整的额外字段).即使我处于开发模式,我手动为参考列表创建了键值字段,因为当引用列表为空时,第一个记录上载不包括该字段(上传空数组会导致另一个错误).
我将在控制台消息之后按相关性顺序包含代码(您将能够看到大多数println).整个项目在github上,如果需要,我可以链接到它或包含更多代码.
相关控制台:
name was set
uploading TestCrewParticipant
with 0 references
if let projects
upload succeeded: TestCrewParticipant
attaching reference
adding TestVoyage:_d147aa657fbf2adda0c82bf30d0e29a9 from guard
references #: Optional(1)
uploading TestCrewParticipant
with 1 references
if let projects
success: 1
uploading TestCrewParticipant
with 1 references
if let projects
success: 1
local storage tested: TestCrewParticipant
u!error for TestCrewParticipant
CKError: <CKError 0x7fcbd05fa960: "Server Record Changed" (14/2004); server message = "record to insert already exists"; uuid = 96377029-341E-487C-85C3-E18ADE1119DF; container ID = "iCloud.com.lingotech.cloudVoyageDataModel">
u!error …Run Code Online (Sandbox Code Playgroud) 所以我有一个功能,在字符串的末尾剥去一个尾随的","(逗号+空格),即使我确保字符串不为空,我也会收到上述错误.以下代码:
print("stripping trailing commas")
for var detail in details {
if detail.contains(",") {
print(detail)
detail.remove(at: detail.endIndex) // <-- Removes last space
detail.remove(at: detail.endIndex) // <-- Removes last comma
}
}
Run Code Online (Sandbox Code Playgroud)
...结果int以下控制台输出:
stripping trailing commas
2016,
fatal error: Can't form a Character from an empty String
Run Code Online (Sandbox Code Playgroud)
第一个实例detail.remove(at: detail.endIndex)正在由调试器突出显示,虽然我无法确定控制台消息中是否存在空间,但我在列表中的每个条目的末尾添加","因此任何实际包含的字符串都是逗号不仅应该有字符(如控制台所指示的那样),但它最后应该有两个字符需要被剥离.
在此先感谢您对导致错误的原因以及如何解决的任何帮助?
目前,我可以使用应用程序委托的didReceiveRemoteNotification方法接收静默推送。
该方法已被弃用,根据此方法,我们应该切换到UNUserNotificationCenter的willPresent方法,但我似乎无法使其在无声推送中起作用。既然没有任何提示可以在无声推送中显示,那么至少可以这样说是不合常理的。
已经阅读了有关推送Xcode 8的注册,不带通知的CKSub以及其他很多东西,但是一切都回到了不推荐的方法。
我们是否应该使用其他方法进行静默推送(这不是面向用户的通知,但在这种情况下,它CKQuerySubscription是触发后台活动的报告)?还是应该willPresent为无声推送工作(在这种情况下,我错过了配置的一部分...)?
提前致谢。
apple-push-notifications swift cksubscription unusernotificationcenter silent-notification
尝试在 Swift 4.2 项目中初始化 CBCentralManager。获取评论中显示的错误:
import CoreBluetooth
class SomeClass: NSObject, CBCentralManagerDelegate {
// Type of expression is ambiguous without more context
let manager: CBCentralManager = CBCentralManager(delegate: self, queue: nil)
// MARK: - Functions: CBCentralManagerDelegate
func centralManagerDidUpdateState(_ central: CBCentralManager) { }
}
Run Code Online (Sandbox Code Playgroud)
如果我self因为nil错误消失而退出,那么我想我错过了一些重要的东西,因为我的一致性CBCentralManagerDelegate......
我可以在没有代表的情况下使用经理吗?如果没有,我需要做什么来解决错误?
正在经历Core Java Fundamentals(Horstmann/Cornell)第9版,但是在第5章左右之后的某个时候,练习文件和示例代码都停止了"主"方法,但这本书的行为就好像它们应该编译和运行等等. .
很困惑,因为java应用程序需要一个主要运行.任何熟悉这些文本的人都可以为我提供一些启示.现在感觉很蠢:)
获取后.partialFailure CKError,我一直在尝试恢复ID和相应的错误,但是我遇到了问题...
现在我正在使用:
print("pE \(error.partialErrorsByItemID) or \(error.userInfo[CKPartialErrorsByItemIDKey])")
if let dictionary = error.userInfo[CKPartialErrorsByItemIDKey] as? [NSObject: Error] {
print("partialErrors #\(dictionary.count)") // <-- Not reaching this...
Run Code Online (Sandbox Code Playgroud)
我也尝试了以下方法:
if let dictionary = error.partialErrorsByItemID { // <-- error.pEBIID returns nil
Run Code Online (Sandbox Code Playgroud)
和:
if let dictionary = error.userInfo[CKPartialErrorsByItemIDKey] as? [CKRecord : CKError /* and Error */] { // <-- but neither triggers the if-let
Run Code Online (Sandbox Code Playgroud)
第一次打印在控制台中显示了这一点(我将打开的标签向左切换,以便它们不会被解释为html):
pE nil or Optional({
">CKRecordID: 0x7b95ace0; CentralTableView:(_defaultZone:__defaultOwner__)>" = ">CKError 0x7a7e4cf0: \"Server Record Changed\" (14/2004); server message = \"record to insert …Run Code Online (Sandbox Code Playgroud)