Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have some variables all coming true just tarih1 output shows
Optional(2016). I didn't resolve it. I want to fix it to 2016.
My code below.
if let myString: String = String(seans.SeanceDate) {
let myStringArrf = myString.componentsSeparatedByString("T")
let tarih: NSString = myStringArrf[0]
let saat: String = String(myStringArrf[1])
let myStringArrf2 = saat.componentsSeparatedByString(":")
let saat1: String = myStringArrf2[0]
let saat2: String = myStringArrf2[1]
cell.saat.text = "\(saat1):\(saat2)"
cell.saat2.text = "\(saat1):\(saat2)"
let myStringArrf23 = tarih.componentsSeparatedByString("-")
let tarih1: NSString = myStringArrf23[0]
let tarih2: NSString = myStringArrf23[1]
let tarih3: NSString = myStringArrf23[2]
let sontarih: String = ("\(tarih3)-\(tarih2)-\(tarih1)")
cell.tarih.text = sontarih
print(sontarih)
}
You use optional binding to find out whether an optional contains a value, and if so, to make that
value available as a temporary constant or variable. Optional binding can be used with if and while
statements to check for a value inside an optional, and to extract that value into a constant or variable,
as part of a single action
if let constantName = someOptional {
// statements
}
if let tarih1 = myStringArrf23[0]{
print(tarih1)
}
Related
This question already has answers here:
Finding index of character in Swift String
(33 answers)
Closed 6 years ago.
This sounds easy, but I am stumped. The syntax and functions of Range are very confusing to me.
I have a URL like this:
https://github.com/shakked/Command-for-Instagram/blob/master/Analytics%20Pro.md#global-best-time-to-post
I need to extract the part #global-best-time-to-post, essentially the # to the end of the string.
urlString.rangeOfString("#") returns Range
Then I tried doing this assuming that calling advanceBy(100) would just go to the end of the string but instead it crashes.
hashtag = urlString.substringWithRange(range.startIndex...range.endIndex.advancedBy(100))
Easiest and best way to do this is use NSURL, I included how to do it with split and rangeOfString:
import Foundation
let urlString = "https://github.com/shakked/Command-for-Instagram/blob/master/Analytics%20Pro.md#global-best-time-to-post"
// using NSURL - best option since it validates the URL
if let url = NSURL(string: urlString),
fragment = url.fragment {
print(fragment)
}
// output: "global-best-time-to-post"
// using split - pure Swift, no Foundation necessary
let split = urlString.characters.split("#")
if split.count > 1,
let fragment = split.last {
print(String(fragment))
}
// output: "global-best-time-to-post"
// using rangeofString - asked in the question
if let endOctothorpe = urlString.rangeOfString("#")?.endIndex {
// Note that I use the index of the end of the found Range
// and the index of the end of the urlString to form the
// Range of my string
let fragment = urlString[endOctothorpe..<urlString.endIndex]
print(fragment)
}
// output: "global-best-time-to-post"
You could also use substringFromIndex
let string = "https://github.com..."
if let range = string.rangeOfString("#") {
let substring = string.substringFromIndex(range.endIndex)
}
but I'd prefer the NSURL way.
use componentsSeparatedByString method
let url = "https://github.com/shakked/Command-for-Instagram/blob/master/Analytics%20Pro.md#global-best-time-to-post"
let splitArray = url.componentsSeparatedByString("#")
your required last text phrase (without # char) will be at the last index of the splitArray , you can concatenate the # with your phrase
var myPhrase = "#\(splitArray[splitArray.count-1])"
print(myPhrase)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to create a folder in DeskSite using the IManage API. can someone please give example.
Any help will be appreciated.
I got answer and hope this will help someone out there.
public void CreateFolder(string matterNo, string clientNo, string dbName, string serviceAccountName)
{
var folderName = "new folder name";
var folderDesc = "new folder description";
var workSpaceName = string.Format("{0}-{1}*", clientNo, matterNo);
if (DMSSession.Connected)
{
IManWorkArea imanWorkArea = DMSSession.WorkArea;
IManDatabase imanDatabase = DMSSession.Databases.ItemByName(dbName);
//workspace search profile values
IManProfileSearchParameters profileParameters = imanWorkArea.Session.DMS.CreateProfileSearchParameters();
profileParameters.Add(IManage.imProfileAttributeID.imProfileAuthor, "*");
//workspace search property values
IManWorkspaceSearchParameters workSpaceParameters = imanWorkArea.Session.DMS.CreateWorkspaceSearchParameters();
workSpaceParameters.Add(IManage.imFolderAttributeID.imFolderOwner, "*");
workSpaceParameters.Add(IManage.imFolderAttributeID.imFolderName, workSpaceName);
IManWorkspace imanWorkSpace = (IManWorkspace)imanDatabase.SearchWorkspaces(profileParameters, workSpaceParameters).ItemByIndex(1);
var workSpaceId= imanWorkSpace.ObjectID;
IManDMS mDms = DMSSession.DMS;
IManWorkspace mWorkSpace = (IManWorkspace)mDms.GetObjectByID(workSpaceId);
IManDocumentFolders mDocFolders = mWorkSpace.DocumentFolders;
IManDocumentFolder mDocFolder = mDocFolders.AddNewDocumentFolder(folderName, folderDesc);
//setting additional prpoerties
mDocFolder.AdditionalProperties.Add("iMan___25", clientNo);
mDocFolder.AdditionalProperties.Add("IMan___26", matterNo);
//setting security
mDocFolder.Security.DefaultVisibility = imSecurityType.imView;
mDocFolder.Security.UserACLs.Add("userName", imAccessRight.imRightAll);
mDocFolder.Update();
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
punch in # 2:00:00 and pushed out at 4:15:20, and the time calculation came out right.. Time = 2:15:20 I want to be able to multiply this with 10.50, and I'm unable to convert time to decimal so I can easily multiply it.
I've done a lots of conversations but none of them made any sense and need help...
would you please help thanks.
Try like this:
let startString = "2:00:00"
let endString = "4:15:20"
func secondsFromMidnight(input:String) -> Int {
let comps = input.componentsSeparatedByString(":")
let h = (Int(comps.first!) ?? 0) * 3600
let m = (Int(comps[1]) ?? 0) * 60
let s = Int(comps.last!) ?? 0
return h + m + s
}
let start = secondsFromMidnight(startString)
let end = secondsFromMidnight(endString)
let elapsedTime = Double( end - start )
let dateComponentsFormatter = NSDateComponentsFormatter()
dateComponentsFormatter.allowedUnits = [.Hour,.Minute,.Second]
dateComponentsFormatter.stringFromTimeInterval(elapsedTime) // "2:15:20"
let result = elapsedTime * 10.5
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying match String patterns in Swift. In my case related to time,
Example
"in 1 week"
"in 4 weeks"
I want to match the whole expression but return especially the number (Int) used between "in" and "week".
What's an efficient way to do this kind of string/pattern matching?
I did use e.g. rangeOfString for simple string matching before but I wonder how to tackle matching these more complex string patterns including its variable part.
Try this:
var pattern = "in\\s+(\\d+)\\s+weeks"
var error: NSError?
var regExp = NSRegularExpression(pattern: pattern, options: .CaseInsensitive, error: &error)!
func getWeekCount(input: String) -> String? {
let m = regExp.matchesInString(input, options: nil, range:NSMakeRange(0, countElements(input)))
print(m)
if let mm = m as? [NSTextCheckingResult] {
print(mm.count)
if mm.count >= 1 {
let range = m[0].rangeAtIndex(1)
return NSString(string: input).substringWithRange(range)
}
}
else {
print("no match")
}
return nil
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This is my string:
(
"2|Apples / amount",
"5|Pizza / pieces",
"10|Oranges / amount",
"1|Brownie / piece"
)
How to get the number, name of the food and unit of the food in arrays
var number:NSMutableArray = NSMutableArray()
var foodName:NSMutableArray = NSMutableArray()
var foodUnit:NSMutableArray = NSMutableArray()
You will need to Split a string into an array.
Not tested but this is the gist of it:
let myFood = ["2|Apples / amount", "5|Pizza / pieces", "10|Oranges / amount", "1|Brownie / piece"]
foreach value in myFood {
let numberAndInfo = myFood.componentsSeparatedByString("|")
// Insert numberAndInfo[0] in your number array.
let foodAndAmount = numberAndInfo[1].componentsSeparatedByString(" / ")
// Insert foodAndAmount[0] into foodName
// Insert foodAndAmount[1] into foodUnit
}
EDIT: Apologize for the indentation.