Swift 3 : List of files Optional() - ios

i'm going nuts.
I'm trying to get the list of "txt" files from a folder but i get the list in the form of
Optional("Filename").txt
Here's my code, nothing fancy. I tried unwrapping the filename but the compiler gives me an error.I tried a "guard", a with "if", i tried "!" ... nothing works.
let enumerator:FileManager.DirectoryEnumerator = FileManager().enumerator(atPath: myFolderPath)!
while let element = enumerator.nextObject() as? String {
print(element)
if element.hasSuffix("txt") {
fileList.append(element)
}
}
I need to show this list in a table view.
I hope i'm not supposed to run trought the array and get the name of the files by using a bunch o string methods just to get rid of this text...
I'm not sure what can i do! I don't really want to use this solution:
Swift: Optional Text In Optional Value
Thank you

# Vadian and Rob: you guys rock, you asked the right question.
The issue was that i'm naming the .txt files with a textfield and i'm doing all the necessary work to make sure that this textfield is not empty or otherwise.
I never thought that this textfield could be optional, hence the Optional("Filename").txt
Once i unwrapped the value from the textfield Optional() had gone.
Thank you.

Related

regular expression for removing empty lines produces wrong results

Can someone help me solve the problem I'm having with a regular expression? I have a file containing the following code:
I'm using a visit to find matches and replace them so that I can remove the empty lines. The result is, however, not what I'm expecting. The code is as follows:
str content = readFile(location);
// Remove empty lines
content = visit (content) {
case /^[ \t\f\v]*?$(?:\r?\n)*/sm => ""
}
This regular expression also removes non empty lines resulting in an output equal to:
Can someone explain what I'm doing wrong with the regular expression as well as the one shown below? I can't seem to figure out why it's not working.
str content = readFile(location);
// Remove empty lines
content = visit (content) {
case /^\s+^/m => ""
}
Kind regards,
Bob
I think the big issue here is that in the context of visit, the ^ anchor does not mean what you think it does. See this example:
rascal>visit ("aaa") { case /^a/ : println("yes!"); }
yes!
yes!
yes!
visit matches the regex at every postfix of the string, so the ^ is relative for every postfix.
first it starts at "aaa", then at "aa" and then at "a".
In your example visit, what will happen is that empty postfixes of lines will also match your regex, and substitute those by empty strings. I think an additional effect is that the carriage return is not eaten up eagerly.
To fix this, simply not use a visit but a for loop or while, with a := match as the condition.

0 Checking if TextBox.Text contains the string in the table. But it doesn't work? Lua

I am making a script inside TextButton script that will check if the TextBox contains any of the word or string inside the table.
text = script.Parent.Parent:WaitForChild('TextBox')
label = script.Parent.Parent:WaitForChild('TextLabel')
a = {'test1','test2','test3'}
script.Parent.MouseButton1Click:connect(function()
if string.match(text.Text, a) then
label.Text = "The word "..text.Text.." was found in the table."
else
label.Text = "The word "..text.Text.." was not found in the table."
end
end)
But it gives an error string expected, got table. from line 7 which is refering to the line if string.match....
Is there any way to get all text in the table?
What's the right way to do it?
Oh boy, there's a lot to say about this.
The error message
Yes.
No, seriously, the answer is yes. The error message is exactly right. a is a table value; you can clearly see that on the third line of code. string.match needs a string as its second argument, so it obviously crashes.
Simple solution
use a for loop and check for each string in a separately.
found = false
for index, entry in ipairs(a) do
if entry == text.Text then
found = true
end
end
if found then
... -- the rest of your code
The better* solution
In Lua, if we want to know if a single element is in a set, we usually take advantage of the fact that tables are implemented as hashmaps, meaning they are very fast when looking up keys.
For that to work, one first needs to change the way the table looks:
a = {["test1"] = true, ["test2"] = true, ["test3"] = true}
Then we can just index a with a string to find out if it is contained int eh set.
if a[text.Text] then ...
* In practice this is just as good as the first solution as long as you only have a few elements in your table. It only becomes relevant when you have a few hundred entries or your code needs to run absolutely as fast as possible.

php str_replace produces strange results

I am trying to replace some characters in a text block. All of the replacements are working except the one at the beginning of the string variable.
The text block contains:
[FIRST_NAME] [LAST_NAME], This message is to inform you that...
The variables are defined as:
$fname = "John";
$lname = "Doe";
$messagebody = str_replace('[FIRST_NAME]',$fname,$messagebody);
$messagebody = str_replace('[LAST_NAME]',$lname,$messagebody);
The result I get is:
[FIRST_NAME] Doe, This message is to inform you that...
Regardless of which tag I put first or how the syntax is {TAG} $$TAG or [TAG], the first one never gets replaced.
Can anyone tell me why and how to fix this?
Thanks
Until someone can provide me with an explanation for why this is happening, the workaround is to put a string in front and then remove it afterward:
$messagebody = 'START:'.$messagebody;
do what you need to do
$messagebody = substr($messagebody,6);
I believe it must have something to do with the fact that a string starts at position 0 and that maybe the str_replace function starts to look at position 1.

'AnyObject' Doesn't have a Member "contactUID" Even thought Intelitype Says it Does?

Another ameture hour Swift Programming Question.
I have been returning values for an object from an array of object "Any Object" "Results". The intellitype says I have an object in the array with a value "ContactUID" however when I try to use ContactUID I get an error saying 'AnyObject' Doesn't contain member 'contactUID'.
The Array called HBCContactList successfully returns, FirstName, LastName and all the other items listed on the screen in the code. However it Will not return the Value 'ContactUID'.
The Model has got the right item. However unlike all the others... ContactUID is a INT64 instead of a string... I have added some screenshots to assit with the process of explaining. Sorry it sounds complicated but I expect I am missing something stupid.
Autocomplete on iOS isn't always accurate, often it will just list all possible selectors / methods.
The root of your problem here is that even though you know HCCContactList holds only HBCDirectoryModel objects, the compiler doesn't as MOContext.executeFetchRequest(freq, error: nil) returns an Array which declares it contains AnyObject's ([AnyObject] / Array<AnyObject>). In order to refer to any of these objects as an HBCDirectoryModel you'll need to conduct a cast to this type.
The easiest way to do this is is to declare your HCCContactList as being an array of HBCDirectoryModel's instead of AnyObject's, and then casting the result of calling MOContext.executeFetchRequest() to this same type.
You can do this as follows
var HCCContactList: Array<HBCDirectoryModel> = []
HCCContactList = MOContext.executeFetchRequest(freq, error: nil) as Array<HBCDirectoryModel>
Or using the shorter syntax
var HCCContactList:[HBCDirectoryModel] = []
HCCContactList = MOContext.executeFetchRequest(freq, error: nil) as [HBCDirectoryModel]

action script 2.0 simple string comparing with conditional statement

So I have this crazy problem with comparing 2 strings in ActionScript 2.0
I have a global variable which holds some text (usually it is "statistic") from xml feed
_root.var_name = fields.firstChild.attributes.value;
when I trace() it it gives me the expected message
trace(_root.var_name); // echoes "statistik"
and when I try to use it in conditional statement the rest of code is not being executed because comparing :
if(_root.overskrift == "statistik"){
//do stuff
}
returns false!
I tried also with:
if(_root.overskrift.equals("statistik"))
but with the same result.
Any input will be appreciated.
Years later but on a legacy project I had the same problem and the solution was in a comment to this unanswered question. So let's make it an anwer:
Where var a:String="some harmless but in my case long string" and var b:String="some harmless but in my case long string" but (a==b) -> false the following works just fine: (a.indexOf(b)>=0) -> true. If the String were not found indexOf would give -1. Why the actual string comparison fails I coudn't figure out either. AS2 is ancient and almost obsolete...

Resources