how to retrieve user setting in iOS? [Xcode 7 + Swift 2] - swift2

I am using Xcode 7 + Swift 2.
I added a setting.bundle to the root of my app with a single text field.
Root.plist looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>StringsTable</key>
<string>Root</string>
<key>PreferenceSpecifiers</key>
<array>
<dict>
<key>Type</key>
<string>PSGroupSpecifier</string>
<key>Title</key>
<string>Controller Settings:</string>
</dict>
<dict>
<key>Type</key>
<string>PSTextFieldSpecifier</string>
<key>Title</key>
<string>Enter Text</string>
<key>Key</key>
<string>key</string>
<key>DefaultValue</key>
<string>Text</string>
<key>IsSecure</key>
<false/>
<key>KeyboardType</key>
<string>NumbersAndPunctuation</string>
<key>AutocapitalizationType</key>
<string>None</string>
<key>AutocorrectionType</key>
<string>No</string>
</dict>
</array>
</dict>
</plist>
I then try to:
Read the default value ("Text")
Change the text field in the iOS settings and then read the new String.
I used this two lines of code:
let settings = NSUserDefaults.standardUserDefaults()
let setting_value = settings.stringForKey("key")! as String
but all i get is this error and the app would not continue:
fatal error: unexpectedly found nil while unwrapping an Optional value
all I want is to read the setting and display the String to a label.
Thanks.
EDIT:
I Understand now that the error is because the value in not assigned yet.
to prevent the error I put 'if' before:
if let name = settings.stringForKey("key") {
print(name)
}
but how can I actually read the value inside "key"?

Related

Alamofire request keeps coming nil

So I am testing my code to see if i get connection with my php service, but I am still getting nil with this simple code:
<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>
The php output is: {"a":1,"b":2,"c":3,"d":4,"e":5}
but when I try it in the app, xcode console shows nil as result.
My alamofire request is:
Alamofire.request(.GET,url).responseJSON{ _,_, result in
print(result.value)
}
I had to override Info.plist because the server I am using is http instead of https with the following:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>primefitness.hostoi.com</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
I am new to swift, and I got no idea on how to debug or track the request. Any suggestions?
EDIT:
I changed "mysite" to the actual site, to avoid confusions
The answer was simple, the hosting im using, uses an analitics javascript that was putting garbage to the end of my json, as stated by Ashish Kakkad, so i just disabled it.

Match everythin between tag as comment

I would like to highlight every thing between
!&
some explanation
&!
As a comment.
I know comment are match with
- name: comment.line.exclamation-mark.fortran
match: (?i)\!.*$
in my fortran.YAML-tmLanguage.
But I don't see how to extend this to the case above.
Since the comments are multiline, you need to split the regex into two expressions, one called begin and one called end.
This allows you to parse multiple lines.
I dont really use YAML, but this code from the C tmLanguage should get you started (Comment style is /* COMMENT */):
<dict>
<key>begin</key> <string>\s*/\*</string>
<key>captures</key>
<dict>
<key>0</key> <dict> <key>name</key>
<string>punctuation.definition.comment.c</string> </dict>
</dict>
<key>end</key> <string>\*/</string>
<key>name</key> <string>comment.block.c</string>
</dict>
So you could use \s\!\& for the begin-tag and \&\! for the end-tag.
I found the below work. Thanks #LinuCC.
# order matter: this block comment syntax should be before the line comment syntax
- name: comment.block.fortran
begin: (?i)\!&
end: (?i)&\!
captures:
'0': {name: comment.line.exclamation-mark.fortran}
- name: comment.line.exclamation-mark.fortran
match: (?i)\!.*$
To make comment toggling work you also need, in another file,
# [PackageDev] target_format: plist, ext: tmPreferences
---
name: Comments
uuid: 40e092f2-ee49-4cbd-8afb-4a5c4f581463
scope: source.fortran
settings:
shellVariables:
- name: TM_COMMENT_START
value: '! '
- name: TM_COMMENT_START_2
value: '!&'
- name: TM_COMMENT_END_2
value: '&!'

Mac OSX 10.9.2, launchd error: "launchctl: Dubious ownership on file (skipping)"

I'm getting the same error launchctl: Dubious ownership on file (skipping): ~.plist
nothing found to load from running a launchctl load command in three different locations as follows, and none of them is working:
sudo launchctl load /Library/LaunchDaemons/updates.novel.plist
sudo launchctl load /Library/LaunchAgents/updates.novel.plist
sudo launchctl load /Users/username/Library/LaunchAgents/updates.novel.plist
Below is my updates.novel.plist file, could you please take a look and let me know what is the problem? thanks
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>GroupName</key>
<string>admin</string>
<key>UserName</key>
<string>Username</string>
<key>Debug</key>
<true/>
<key>Label</key>
<string>updates.novel</string>
<key>ProgramArguments</key>
<array>
<string>/Applications/AMPPS/php-5.3/bin/php</string>
<string>/Applications/AMPPS/www/files/allnovels/novel.php</string>
<string>--daemon</string>
</array>
<key>StandardErrorPath</key>
<string>/var/log/files/error.1.log</string>
<key>StandardOutPath</key>
<string>/var/log/files/error.2.log</string>
<key>RunAtLoad</key>
<true/>
<key>AbandonProcessGroup</key>
<true/>
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>14</integer>
<key>Minute</key>
<integer>0</integer>
</dict>
</dict>
</plist>
launchd services need to be started by the user who owns the plist file. If the owner is not root, then the service must not be launched with sudo.
Also, the permissions on the file must deny write access to all users except the owner.
Finally, the file must be a regular file (ie not a pipe or a socket or anything else).
In man launchctl we can read:
Note that per-user configuration files (LaunchAgents) must be owned by the user loading them. All sytem-wide daemons (LaunchDaemons) must be owned by root. Configuration files must not be group- or world-writable. These restrictions are in place for security reasons.
This is how launchctl.c checks that:
bool path_goodness_check(const char *path, bool forceload) {
if (forceload) {
return true;
}
if (sb.st_mode & (S_IWOTH|S_IWGRP)) {
fprintf(stderr, "%s: Dubious permissions on file (skipping): %s\n", getprogname(), path);
return false;
}
if (sb.st_uid != 0 && sb.st_uid != getuid()) {
fprintf(stderr, "%s: Dubious ownership on file (skipping): %s\n", getprogname(), path);
return false;
}
if (!(S_ISREG(sb.st_mode) || S_ISDIR(sb.st_mode))) {
fprintf(stderr, "%s: Dubious path. Not a regular file or directory (skipping): %s\n", getprogname(), path);
return false;
}
if ((!S_ISDIR(sb.st_mode)) && (fnmatch("*.plist", path, FNM_CASEFOLD) == FNM_NOMATCH)) {
fprintf(stderr, "%s: Dubious file. Not of type .plist (skipping): %s\n", getprogname(), path);
return false;
}
return true;
}
So in other words, correct the ownership, permissions or path of your .plist file or force the loading (-F).

Reading xml nodes using VB script

I have an xml file that I want to read using VBScript (Technology limitation). Below is the code and xml file. I am able to read the file if there is no DTD element involved but the code doesn't work for file having DTD and xml-style element.
Code-
Dim xmlDoc1:Set xmlDoc1 = CreateObject("MSXML2.DomDocument")
xmlDoc1.async=False
xmlDoc1.load "C:\ABC.xml"
Dim xmlTCID:Set xmlTCID = xmlDoc1.selectNodes("//*")
For nNodeCount = 0 To xmlTCID.length
MsgBox(xmlTCID(nNodeCount).nodeName)
Next
ABC.xml -
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE RESULT SYSTEM "Result.dtd"[]>
<?xml-stylesheet type="text/xsl" href="Result.xsl"?>
<SUMMARY>
<TITLE>Test</TITLE>
</SUMMARY>
<IDS>
<DATA>
<NAME>A</NAME>
<VALUE>PASS</VALUE>
</DATA>
<DATA>
<NAME>B</NAME>
<VALUE>PASS</VALUE>
</DATA
<DATA>
<NAME>C</NAME>
<VALUE>FAIL</VALUE>
</DATA
</IDS>
<IDS>
<DATA>
<NAME>A</NAME>
<VALUE>PASS</VALUE>
</DATA>
<DATA>
<NAME>B</NAME>
<VALUE>FAIL</VALUE>
</DATA
</IDS>
Note - If I avoid -
<!DOCTYPE RESULT SYSTEM "Result.dtd"[]>
<?xml-stylesheet type="text/xsl" href="Result.xsl"?>
The above code is able to read the nodes but with the above two lines in xml file, it gives the below error -
Requirement - I need to read the name of last DATA node with FAIL for each IDS node.
Any suggestion as what to do to get the code working even with -
<!DOCTYPE RESULT SYSTEM "Result.dtd"[]>
<?xml-stylesheet type="text/xsl" href="Result.xsl"?>
As there are problems with your XML - more than one top level element, miising ">" - setting the ProhibitDTD Property to False won't solve all of your tasks.
xmlDoc.validateOnParse=False
worked for me.

Replace all occurrences except the first in Ruby. The regular expression spans multiple lines

I am trying to down my last 3200 tweets in groups of 200(in multiple pages) using restclient gem.
In the process, I end up adding the following lines multiple times to my file:
</statuses>
<?xml version="1.0" encoding="UTF-8"?>
<statuses type="array">
To get this right(as the XML parsing goes for a toss), after downloading the file, I want to replace all occurrences of the above string except the first.
I am trying the following:
tweets_page = RestClient.get("#{GET_STATUSES_URL}&page=#{page_number}")
message = <<-MSG
</statuses>
<?xml version="1.0" encoding="UTF-8"?>
<statuses type="array">
MSG
unless page_number == 1
tweets_page.gsub!(message,"")
end
What is wrong in the above? Is there a better way to do the same?
I believe it would be faster to download the whole bunch at once and split the body of your response by message and add it for the first entry.
Something like this, can't try it out so consider this just as an idea.
tweets_page = RestClient.get("#{GET_STATUSES_URL}").body
tweets = tweets_page.split(message)
tweets_page = tweets[0]+message+tweets[1..-1]
You could easily break them up in groups of 200 like that also
If you want to do it with a gsub on the whole text you could use the following
tweets_page = <<-MSG
first
</statuses>
<?xml version="1.0" encoding="UTF-8"?>
<statuses type="array">
second
</statuses>
<?xml version="1.0" encoding="UTF-8"?>
<statuses type="array">
rest
MSG
message = <<-MSG
</statuses>
<?xml version="1.0" encoding="UTF-8"?>
<statuses type="array">
MSG
new_str = tweets_page.gsub message do |match|
if defined? #first
""
else
#first = true
message
end
end
p new_str
gives
type=\"array\">\nrest\n"
"first\n</statuses>\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<statuses type=\"array\">\nsecond\nrest\n"

Resources