小编Ken*_*ong的帖子

如何在Swift 4中获取具有特定范围的子字符串?

这是使用官方Swift4文档中的示例代码

let greeting = "Hi there! It's nice to meet you! "
let endOfSentence = greeting.index(of: "!")!
let firstSentence = greeting[...endOfSentence]
// firstSentence == "Hi there!"
Run Code Online (Sandbox Code Playgroud)

但是,让我们说let greeting = "Hello there world!" ,我想在这句话中只检索第二个单词(substring)?所以我只想要"那里"这个词.

我试过用"世界!" 作为一个参数, let endOfSentence = greeting.index(of: "world!")!但Swift 4 Playground并不喜欢这样.它期待'Character',我的论点是一个字符串.

那么如何才能得到一个非常精确的子范围的子串呢?或者在句子中加上第n个单词以便将来使用?

substring swift swift4

9
推荐指数
3
解决办法
2万
查看次数

在尝试创建文本文件操纵器时,文本未附加到新文件中

问题和我所处的位置:我无法将文本附加到我使用该程序创建的这些新文件中.目前它只复制文件但不附加它们.见评论行 "// append file name into the new file ".

其次,最终转储文件似乎只附加.java文件,它不读取或附加输入文件.

我正在尝试做的解释:

长和短是我试图制作一个程序,将其放入随机文件夹中,其中.txt文件填充数据.

我需要该程序

  • 查看它所在的文件夹范围内
  • 然后获取任何.txt文件和
    a)制作副本,但将原始文件名附加到文本正文中(在顶部),在"<filenamehere.txt"之类的子文件夹中将其附加到正文中(在顶部)

    b)然后复制original.txt的正文内容c)获取前面的.txt文件并将其附加到单个dump.txt文件中d)对所有本地txt文件重复此操作并继续追加到dump.txt文件的末尾

所以最后,如果我有7个文件,我将有7个附加副本和1个巨大转储文件,其中包含7个附加副本的所有内容.例如,如果我有三个文本文件,每个文件只有一个单词.所以a.txt,b.txt,c.txt和三个单词都是"Hello world!".a.txt副本将包含内容

"> A.TXT

你好

"

现在它只是复制Hello(原始正文内容)但不附加> a.txt.最终的转储文本文件不会累积其他文件中的任何内容,但奇怪的是从.java文件中获取源代码.基本上,我得到一个// Output文件夹,里面是.txt文件的副本和megaDump.txt,它们设法获取.java文本,但没有附加其他文本文件.

import java.io.* ;
import java.nio.file.*;

public class FilePrepender // class name 
{
    public static void main (String [] args)
    {
        // make a giant dump file which we will append all read files into  
        try {
            new File("Output\\").mkdirs();
            File megaDumpFile = new File("Output\\masterDump.txt");

            if (megaDumpFile.createNewFile()) {
                System.out.println("File creation success");
            } else …
Run Code Online (Sandbox Code Playgroud)

java

5
推荐指数
1
解决办法
95
查看次数

iOS 11 用户自定义声音通知

我在实现 Apple 的基本示例代码时遇到了麻烦,如下所示

https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/SchedulingandHandlingLocalNotifications.html#//apple_ref/doc/uid/TP40008194-CH5-SW3

这是我的代码;这是非常“教科书上的例子”。我当前收到错误“'init(named:)' 不可用”

    let content = UNMutableNotificationContent()
    content.title = NSString.localizedUserNotificationString(forKey:"testreminder!", arguments: nil)
    content.body = NSString.localizedUserNotificationString(forKey:"Reminder body.", arguments: nil)

    content.sound = UNNotificationSound(named: "test.aiff");
    content.categoryIdentifier = "myCategory"

    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
    let request = UNNotificationRequest(identifier: "test.aiff", content: content, trigger: trigger)
    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
Run Code Online (Sandbox Code Playgroud)

编辑:在尝试建议后,这是代码,但它不起作用。它给出“init(named:)”不可用

    let content = UNMutableNotificationContent()
    content.title = NSString.localizedUserNotificationString(forKey:"testreminder!", arguments: nil)
    content.body = NSString.localizedUserNotificationString(forKey:"Reminder body.", arguments: nil)

    content.sound = UNNotificationSound.init(named: "test.aiff")
    content.categoryIdentifier = "myCategory"

    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
    let request = …
Run Code Online (Sandbox Code Playgroud)

ios swift watchkit

4
推荐指数
1
解决办法
6645
查看次数

标签 统计

swift ×2

ios ×1

java ×1

substring ×1

swift4 ×1

watchkit ×1