Posts

Post marked as solved
5 Replies
2.4k Views
I'm using a UIPageViewController to display various text files. My app uses the Xcode template for a page-based app.At the moment, when a new file is loaded the controller displays the old text until a page is turned. It then displays the new text, so the data is being loaded properly. What I'd like the controller to do is display the first page of the new file immediately upon loading. I've defined a notification that's sent when a new text file is loaded. What should my page view controller do when it receives this notification?
Posted
by Albinus.
Last updated
.
Post marked as solved
4 Replies
5.5k Views
I'm trying to set up a text view to show lines of text, along with line numbers on the right margin. The string for the text view would look something like this:"Text of first line" + "Line 1" + "\n" + "Text of second line" + "Line 2" + "\n"I want the end result to look something like this:Into my heart an air that kills, 1From yon far country blows: 2What are those blue remembered hills, 3What spires, what farms are those? 4(Sorry about the crooked right column: it looked fine in the message editor. The numbers should all be in a straight column, aligned to the right margin of the text view.) Is it possible to use an NSAttributedString to do this? I've tried several different ways of constructing such a string, but no luck so far. I'm beginning to think this is the wrong approach entirely. What is the best way to get the kind of layout that I want?
Posted
by Albinus.
Last updated
.
Post not yet marked as solved
2 Replies
3.5k Views
In my storyboard I have a UITextView pinned to the top and bottom safe areas of its view, and with a constraint that centres it. The various text files it will be displaying have lines of different lengths (e.g. some are poetry, others are prose), so I'd like the text view to adjust its width dynamically in response to its content. I've found some stuff online that shows how to do this for dynamic height, but I can't find anything that shows how to set constraints for dynamic width. Can this be done in a storyboard at all?
Posted
by Albinus.
Last updated
.
Post marked as solved
8 Replies
1.3k Views
I need to scan some text files and separate the bytes into those with the sign bit set and those without. I'd like to use a scanner to do this. How would I define a character set for this scanner?(The reason I need to do this is that the metadata for the text is encoded into bytes with the sign bit set. These files date back to the 80's.)
Posted
by Albinus.
Last updated
.
Post marked as solved
7 Replies
647 Views
I want to scan through a legacy ASCII text file that has diacritics encoded with plain ASCII characters. I want to transform this file into another file that has Unicode characters for the diacritics. I've got a Dictionary that maps the ASCII encodings of the diacritics to the proper Unicode character. One complication is that often two or three of these diacritics are applied to the same character. But the Dictionary covers all the combinations.Here is an example of the code I've come up with:let diacriticsDictionary: Dictionary<String,String> // This is read from a file. let textString = #"The quick brown fo/)x jumped ove\r the lazy dog."# let diacritics = #"\/)(|"# let diacriticsSet = CharacterSet(charactersIn: diacritics) let reader = Scanner(string: textString) for index in 0 ..< textString.count { var key: NSString? let c = textString[index] // ERROR if diacritics.contains(c) { reader.scanLocation = index reader.scanCharacters(from: diacriticsSet, into: &key) newTextString.append(diacriticsDictionary[String(key!)]!) index = reader.scanLocation } else { newTextString.append(c) } }I need to use the scanner to get all of a sequence of diacritic characters into the key variable in order to do the dictionary lookup. So that means (I think) I have to loop using the integer index to be able to tell the scanner where to start.Of course I get a cannot subscript String with an Int error on line 10: I put that code there just to illustrate what I'm trying to do. But if I loop over the characters instead, how would I tell the scanner where to start scanning in the string?I'm also annoyed that on line 11 I can't use the character set to look at c. I suspect there's a much neater way of doing this using Scanner or NSScanner alone, but I'm unable to see it.
Posted
by Albinus.
Last updated
.
Post not yet marked as solved
2 Replies
398 Views
I'm trying to post a reply to one of my own posts, which explains a possible solution to the original problem I was asking about. It contains a bit of code, but no web addresses of any kind. Yet it was set aside for moderation, and still has not been posted after several hours. What reasons could there be for my post being flagged for moderation?I don't recall having this happen before. I certainly haven't been insulting anyone.
Posted
by Albinus.
Last updated
.
Post marked as solved
7 Replies
574 Views
I have a bunch of strings that contain postfix operators. I want to transform all of these operators into prefixes. For example, suppose 1 and 2 are the postfixes. I want to transform the string abcD12efg into abc12Defg.I know about swapAt, but after trying many different loops in a playground, I've gotten myself confused about strings, arrays, indexes, iterators, etc. (I'm doing all of this in Swift 5, blessings be upon it.) Any help would be appreciated.
Posted
by Albinus.
Last updated
.
Post marked as solved
6 Replies
1.1k Views
I want to learn more about functional programming. I decided to start with an "easy" example: getting the Cartesian product of two arrays, as in this code:let xArray = [1, 2, 3] let yArray = [4, 5, 6] var pairs: [(Int, Int)] = [] for x in xArray { for y in yArray { pairs.append((x, y)) } }Now, many hours later, I still haven't figured out how to do this in a functional way. Perhaps I should stick to declarative programming.
Posted
by Albinus.
Last updated
.
Post not yet marked as solved
8 Replies
694 Views
I'm using NLTokenizer in this code to extract words from text files:func loadData() { var wordTokens: Set<String> = [] let tokenizer = NLTokenizer(unit: .word) tokenizer.string = TextContent.sharedInstance.text.uppercased() let tokenRanges = tokenizer.tokens(for: tokenizer.string!.startIndex..<tokenizer.string!.endIndex) for r in tokenRanges { let word = String(tokenizer.string![r]).trimmingCharacters(in: .whitespacesAndNewlines) if word.count > 0 { wordTokens.insert(word) } }It's been working fine for most files, including some that are over 800KB in size. But when I input an even larger one (1.4MB), I don't get anything in the tokenRanges array at line #5. I've checked the tokenizer string, and it is initialized.I have a limited understanding of threads. But I'm wondering whether the tokenizer starts a background thread at line #5 to do its work, and this thread isn't complete yet when line #6 is executed. If this is what's happening, is it possible to somehow require the thread to complete before proceeding?I've also tried this using the enumerateTokens function with a closure at line #5, with the same result.
Posted
by Albinus.
Last updated
.
Post not yet marked as solved
4 Replies
670 Views
I've added a custom menu "Statistics" to my app menu. I know the actions for its items can be placed in the AppDelegate file. But if possible I'd like to put these actions in a separate file, to simplify things. Can I add a new file to my project that will let me connect my custom menu items to the actions defined within it?
Posted
by Albinus.
Last updated
.
Post not yet marked as solved
5 Replies
593 Views
Is this the best way to ensure the computations for the properties are only done once?struct GenericDataFrequency<T: Hashable & Comparable> { let data: [T] let frequencies: [T : Int] let sortedFrequencies: [(T, Int)] init(data: [T]) { self.data = data frequencies = Dictionary((data.map {($0, 1)}), uniquingKeysWith: {$0 + $1}) sortedFrequencies = Array(frequencies).sorted {$0.key < $1.key} } func frequency(of elements: Set<T>) -> [T : Int] { return Dictionary(uniqueKeysWithValues: elements.map {($0, frequencies[$0] ?? 0)}) } }
Posted
by Albinus.
Last updated
.
Post marked as solved
1 Replies
363 Views
Is there a purely functional way of writing frequency(of:) in this code?struct GenericDataFrequency<T: Hashable & Comparable> { var data: [T] var frequencies: [T : Int] { return Dictionary((data.map {($0, 1)}), uniquingKeysWith: {$0 + $1}) } func frequency(of elements: [T]) -> [T : Int] { var collector = [T : Int]() for e in elements { collector[e] = frequencies[e] ?? 0 } return collector } }I've been staring at it for ages and don't see it. This could be a valuable learning experience for me.
Posted
by Albinus.
Last updated
.
Post marked as solved
1 Replies
1.5k Views
I use this code quite a lot in playgrounds to set up a test array:var testArray = [Int]() for _ in 1 ... 1000 { testArray.append(Int.random(in: 1 ... 9)) }It runs fairly quickly. But is there any way I could save the generated array in the playground, so it wouldn't have to be recreated for each test run?
Posted
by Albinus.
Last updated
.
Post marked as solved
9 Replies
741 Views
A closure insists on having a function with the signature:(_) -> _I don't understand what this means. I thought it would be the same as (Any) -> Any, but the compiler won't accept the function I'm trying to use, which looks like (T) -> Range<T> . The generic T has been declared as Comperable.
Posted
by Albinus.
Last updated
.