retrieving id and incrementing it swift3 - ios

I have a id whose format is 1223-3939-ABC.1 and I would like to retrieve the last value i.e. 1 and increment it so now it looks like 1223-3939-ABC.2. But its possible that "1" is not there so in that case, I would like to append ".1"
I am trying to achieve this in Swift and here is my code:
var deviceId: String = "1234-ASCD-SCSDS.1"
if (deviceId != "") {
var id: [String] = deviceId.components(separatedBy: ".")
if let incrementedId: String = id.capacity > 1 ? deviceId.components(separatedBy: ".")[1] : "" {
if (incrementedId == "") {
//should append to id
var firstEle = deviceId.components(separatedBy: ".")[0]
firstEle.append(".")
firstEle.append("1")
deviceId = firstEle
} else {
// retrieve that id, convert to int, increment id, convert back to string and replace the old id with new id
let newId: Int = Int(deviceId.components(separatedBy: ".")[1])! + 1
deviceId = deviceId.replacingOccurrences(of: ".\\d", with: ".\(newId)", options: .regularExpression)
}
}
}
Not sure what I'm doing wrong?

Your regular expression for replacing is .\\d where . is actually any symbol. Replace it with \\.\\d and it will operate as expected.
You are referencing capacity but you need to reference count to understand an amount of components.
Based on documentation:
Capacity – the total number of elements that the array can contain without
allocating new storage.

There are several problems, such as
Wrong usage of capacity (as already said by Nikita),
Your code assumes that there is only a single dot, so that id
has exactly two elements.
Your code will crash if the dot is not followed by an integer.
The main problem is that
deviceId = deviceId.replacingOccurrences(of: ".\\d", with: ".\(newId)", options: .regularExpression)
replaces all occurrences of an arbitrary character followed by
any digit with ".\(newId)". It should probably be
deviceId = id[0] + ".\(newId)"
instead.
But the entire problem can be solved much easier:
Find the last occurrence of a dot.
Check if the part of the string following the dot can be converted to an integer.
If yes, replace that part by the increased integer, otherwise append .1
Both checks can be achieved with conditional binding, so that the
if-block is only executed if the device id already has a trailing
number:
var deviceId = "1234-ASCD-SCSDS.1"
if let pos = deviceId.range(of: ".", options: .backwards),
let id = Int(deviceId.substring(from: pos.upperBound)) {
deviceId = deviceId.substring(to: pos.upperBound) + String(id + 1)
} else {
deviceId = deviceId + ".1"
}
print(deviceId) // 1234-ASCD-SCSDS.2

Related

How Can I Construct an Efficient CoreData Search, Including Allowing For Preceding and Trailing Characters Here?

Based on straight SQL searches in a previous app, I am adding CoreData searching to a new app. These searches are in a custom dictionary db that the app contains; this function does the work:
public func wordMatcher (pad: Int, word: Array<String>, substitutes : Set<String> ) {
let context = CoreDataManager.shared.persistentContainer.viewContext
var query: Array<String>
var foundPositions : Set<Int> = []
var searchTerms : Array<String> = []
if word.count >= 4 {
for i in 0..<word.count {
for letter in substitutes {
query = word
query[i] = letter
searchTerms.append(query.joined())
let rq: NSFetchRequest<Word> = Word.fetchRequest()
rq.predicate = NSPredicate(format: "name LIKE %#", query.joined())
rq.fetchLimit = 1
do {
if try context.fetch(rq).count != 0 {
foundPositions.insert(i)
break
}
} catch {
}
}
// do aggregated searchTerms search here instead of individual searches?
}
}
}
The NSFetchRequest focuses on one permutation at a time. But I'm accumulating the search string fragments in the array searchTerms because I don't know if it would be more efficient to construct a single query connected with ORs, and I also don't know how to do that in CoreData.
The focus is on the positions in the original term word: I need to indicate if any given location has at least one of the substitutes as a valid fit. So to implement the aggregate searchTerms approach, a FetchRequest would have to happen for each location in the base term.
A second complication is the one referred to in the title of the question. I am using LIKE because the search term in the FetchRequest could be a substring in a longer word. However, the maximum number of letters is 11, and pad is the starting point of the original term in that field of 11 spaces.
So if pad is 3, then I would need to allow for 0..<pad preceding characters. And because there may be trailing characters, I would also want results with 0..<(11 - (pad + word.count)) alphabetic characters after the last letter in the search term.
Regex seems like one way to do this, but I haven't found a clear example of how to do this in this case, and especially with the multiple search terms (if that's the way to go). The limits of SQLite in the previous version forced constructing multiple queries with increasing numbers of "_" underscores to indicate the padding characters; that tended to really explode the number of queries.
BTW, substitutes is limited to an absolute maximum of 9 values, and in practice is usually below 5, so things are a little more manageable.
I would like to get a grip on this, and so if anyone can provide direction or examples that can make this a reasonably efficient function, the help is appreciated greatly.
EDIT:
I've realized that I need a result for each position in the target string, with cases where the leading and trailing spaces also may need to contain a substitute as well.
So I'm moving to this:
public func wordMatcher (pad: Int, word: Array<String>, substitutes : Set<String> ) {
let context = CoreDataManager.shared.persistentContainer.viewContext
var pad_ = pad
var query: Array<String>
var foundPositions : Set<Int> = []
let rq: NSFetchRequest<Word> = Word.fetchRequest()
rq.fetchLimit = 1
let subs = "[\(substitutes.joined())]"
// if word.count >= 4 { // because those locations will be blocked off anyway otherwise
let start = pad > 0 ? -1 : 0
let finish = 11 - (pad + word.count) > 0 ? word.count + 1 : word.count
for i in start..<finish {
query = word
var _pad = 11 - (pad + word.count)
if i == -1 {
query = Array(arrayLiteral: subs) + query
pad_ -= 1
} else if i > word.count {
query.append(subs)
_pad -= 1
} else {
pad_ = pad
query[i] = subs
}
let endPad = _pad > 0 ? "{0,\(_pad)}" : ""
let predMatch = ".\(query.joined())\(endPad)"
print(predMatch)
rq.predicate = NSPredicate(format:"position <= %# AND word MATCHES %#", pad_, predMatch)
do {
if try context.fetch(rq).count != 0 {
foundPositions.insert(i)
}
} catch {
}
// }
}
lFreq = foundPositions
}
This relies on a regex substitution, inserted into the original target string. What I'll have to find out is if this is fast enough at the edge cases, but it may not be critical even in the worst case.
predMatch will end up looking something like "ab[xyx]d{0,3}", and I think I can get rid of the position section by changing it to be "{0,2}ab[xyx]d{0,3}". But I guess I'm going to have to try to find out.

Realm Swift adding object with String property having character count > 1874 is crashing

I have a csv file with 200K rows.
each row contains 9 values, all the rows String character count is less than 2048 but 3 rows have character count equal to 4420. These rows indexes are 134481, 134482, 134483 respectively. Out of the 9 values in the row last string value is big.
When row 134481 is adding to realm or creating in realm, it get crashed at BpTree::create_root_from_mem with EXC_BAD_ACCESS(code=2, address=xxxxx)
Below is the swift code [swift version 2.2, xcode: 7.3, realmSwift: 1.1.0]...for adding the csv objects to realm database.
This code is running in a background queue. I tried skipping first 130k rows and even then it crashes exactly at row 134481. Only after reducing the string character count the object is added without crash.
class PackageObject:Object {
dynamic var id = ""
dynamic var packageBatch = ""
dynamic var packageCode = ""
dynamic var packageDescription = ""
dynamic var packageName = ""
dynamic var packagelocalName = ""
dynamic var packageNumber = ""
dynamic var packageBuild = ""
dynamic var packageSummary = ""
}
let filename = NSBundle.mainBundle().pathForResource("codes", ofType: "tsv")
if let realm = try? Realm(configuration: configuration), data = try? String.init(contentsOfFile: filename!) {
let block = 5000
var lineNumber = 0
print(realm.configuration.fileURL)
data.enumerateLines({ (line, stop) in
if lineNumber % block == 0 {
print("begin: ", lineNumber)
realm.beginWrite()
}
lineNumber += 1
var strings = line.componentsSeparatedByString("\t")
if lineNumber >= 134481 && lineNumber <= 134483 {
// strings[8] count is about 3805
// after reducing the count to 1874, the object
// is successfully added to the realm, else a crash is observed.
let count = strings[8].characters.count
print(count, line.characters.count)
let index = strings[8].endIndex.advancedBy(1931 - count)
strings[8].removeRange(index..<strings[8].endIndex)
}
let packageObj = PackageObject(value: strings)
realm.add(packageObj)
//realm.create(PackageObject.self, value:strings)
if lineNumber % block == 0 {
print("commit: ", lineNumber)
_ = try? realm.commitWrite()
}
})
}
// The last commitWrite is not handled...inside the block.
// for the below condition...
if lineNumber % block != 0 {
_ = try? realm.commitWrite()
}
The realm doc says a String property can be less than 16MB, but here its not even taking 10KB.
I don't find anything wrong with the string, it is just plain English text. Is this a bug or do I have to add objects in a different way.
If the string property is indexed, upgrading to 2.0 will probably fix this.
Older versions had a flaw in how the index was implemented that lead to it hitting a stack overflow when strings have sufficiently long common prefixes, and 2.0 changed how indexes are stored to remove the recursion.

How to put variable inside text field AND how to convert all elements to string NOT just 1 at a time?

This is a follow up question to How to have 10 characters total and make sure at least one character from 4 different sets is used randomly
this is my code so far
let sets = ["ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz", "1234567890", "\"-/:;()$&#.,?!'[]{}#%^\\|~<>€£¥•.,"].map { Array($0.characters) }
var randoms = sets.map { $0.random }
while randoms.count < 10 {
randoms.append(sets.random.random)
}
var convertedElems = String()
let something = randoms.shuffled()
for key in something {
convertedElems = String(key)
}
uniqueRoomID.text = randoms.shuffled()
Im getting an error saying cannot convert [Element] to type "String"
So i tried a for loop but that only converts 1 at a time when its supposed to do all 10
my other question is i tried storing a character in a variable and then setting a text field.text equal to that variable and nothing happened
What am i doing wrong here
Your randoms.shuffled() is an array of Characters. You need to convert it back into a String.
Change this:
uniqueRoomID.text = randoms.shuffled()
to this:
uniqueRoomID.text = String(randoms.shuffled())

Add string to beginning of another string

Basic question
I have 2 strings. I want to add one string to another? Here's an example:
var secondString= "is your name."
var firstString = "Mike, "
Here I have 2 strings. I want to add firstString to secondString, NOT vice versa. (Which would be: firstString += secondString.)
More detail
I have 5 string
let first = "7898"
let second = "00"
let third = "5481"
let fourth = "4782"
var fullString = "\(third):\(fourth)"
I know for sure that third and fourth will be in fullString, but I don't know about first and second.
So I will make an if statement checking if second has 00. If it does, first and second won't go in fullString. If it doesn't, second will go intofullString`.
Then I will check if first has 00. If it does, then first won't go inside of fullString, and if not, it will go.
The thing is, I need them in the same order: first, second, third fourth. So in the if statement, I need a way to potentially add first and second at the beginning of fullString.
Re. your basic question:
secondString = "\(firstString)\(secondString)"
or
secondString = firstString + secondString
Here is a way to insert string at the beginning "without resetting" per your comment (first at front of second):
let range = second.startIndex..<second.startIndex
second.replaceRange(range, with: first)
Re. your "more detail" question:
var fullString: String
if second == "00" {
fullString = third + fourth
} else if first == "00" {
fullString = second + third + fourth
} else {
fullString = first + second + third + fourth
}
From the Apple documentation:
String values can be added together (or concatenated) with the addition operator (+) to create a new String value:
let string1 = "hello"
let string2 = " there"
var welcome = string1 + string2
// welcome now equals "hello there"
You can also append a String value to an existing String variable with the addition assignment operator (+=):
var instruction = "look over"
instruction += string2
// instruction now equals "look over there"
You can append a Character value to a String variable with the String type’s append() method:
let exclamationMark: Character = "!"
welcome.append(exclamationMark)
// welcome now equals "hello there!"
So you are pretty much free to add these in any way shape or form.
Which includes
secondstring += firststring
Edit to accommodate the new information:
Strings in Swift are mutable which means you can always add to a string in-place without recreating any objects.
Something like (pseudo-code)
if(second != "00")
{
fullstring = second + fullstring
//only do something with first if second != 00
if(first != "00")
{
fullstring = first + fullstring
}
}

Short Random Unique alphanumeric keys similar to Youtube IDs, in Swift

Is there a way to create random unique IDs similar to the YouTube IDs in Swift?
I know there are similar answers on this link, but they are for Php. But I want something in Swift.
I have tried using timestamp and UUIDs, but I want an alphanumeric short keys which would be around 4-10 characters so users can easily share with others verbally.
Thanks.
Looking for just a unique string
You can use UUIDs they're pretty cool:
let uuid = NSUUID().UUIDString
print(uuid)
From the  docs
UUIDs (Universally Unique Identifiers), also known as GUIDs (Globally
Unique Identifiers) or IIDs (Interface Identifiers), are 128-bit
values. UUIDs created by NSUUID conform to RFC 4122 version 4 and are
created with random bytes.
Some info about uuid: https://en.wikipedia.org/wiki/Universally_unique_identifier
Looking for a more specific length
Try something like this:
func randomStringWithLength(len: Int) -> NSString {
let letters : NSString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
let randomString : NSMutableString = NSMutableString(capacity: len)
for _ in 1...len{
let length = UInt32 (letters.length)
let rand = arc4random_uniform(length)
randomString.appendFormat("%C", letters.character(at: Int(rand)))
}
return randomString
}
But i'll keep my answer incase someone else stumbles upon this looking for a UUID
This will allow you to create a random short code. It can create codes from Hexadecimal all the way to base 62 codes and of varying lengths.
aka.
let myCode = ShortCodeGenerator.getCode(length: 6)
3dH7t8,
fdE7j1,
Gl6jKd,
Hv8gU3,
let myBase32Code = ShortCodeGenerator.getCode(withBase: UInt32(32), length: 6)
3HF75J,
J67N9D,
B47SO3,
L9SD2N
You would have to check for redundancy and then create a new one if it has already been used.
struct ShortCodeGenerator {
private static let base62chars = [Character]("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".characters)
private static let maxBase : UInt32 = 62
static func getCode(withBase base: UInt32 = maxBase, length: Int) -> String {
var code = ""
for _ in 0..<length {
let random = Int(arc4random_uniform(min(base, maxBase)))
code.append(base62chars[random])
}
return code
}
}
This answer was useful in creating the above code and also has good
information on the number of unique identifiers you can have with each base number and length.
The total number of unique identifiers you need can be calculated by the equation:
BaseNumber^length = # of unique IDs
EDIT:
I have added even more functionality for converting Int's and NSDate's to shortcodes for my own project as well and put those into a project on GitHub.
Updated for swift 3:
If you want to generate Short Random Unique alphanumeric keys, used below lines of codes;
//function defination:
func generateTransactionId(length: Int) -> String {
var result = ""
let base62chars = [Character]("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".characters)
let maxBase : UInt32 = 62
let minBase : UInt16 = 32
for _ in 0..<length {
let random = Int(arc4random_uniform(UInt32(min(minBase, UInt16(maxBase)))))
result.append(base62chars[random])
}
return result
}
//function call:
let mTranactionId = self.generateTransactionId(length: 5) // you can change the value of length as you want
print("mTranactionId: \(mTranactionId)")
Ex: Result looks like: XHA7K, QTC92, MS1PT, YE2PV
//Enjoy coding...!
Does NSUUID().UUIDString do what you need?
I'm very pleased with NanoID.
https://github.com/ai/nanoid
...and here's the SWIFT version:
https://github.com/antiflasher/NanoID
You just need to drag 1 file (NanoID.swift) into your project, and you are good to go!
TAGS: Short GUID, Short UUID, Short Unique ID, Swift, iOS
Swift custom alternative(these version doesn't check for duplicate char):
func randomString(length: Int) -> String {
let letters = Array("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
var randomString: String = ""
for _ in 0..<length {
let randomNumber = Int.random(in: 0..<letters.count)
randomString.append(letters[randomNumber])
}
return randomString
}
You can archive this just using UUID.
If you don't want the whole string, as any String in Swift, you select a small portion of the string by using range, like this:
let text = UUID().uuidString
let index = text.index(text.startIndex, offsetBy: 8)
let small = text[text.startIndex..<index]
Notice 8 is the length of string I suggested, you can improve this by clamping this value using min(size, text.count) for example.
And finally, small is a Substring, to cast it to String just cast as usual - String(small)
Swift 5 version using a String extension
extension String {
static func random(length: Int) -> String {
let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
return String((0..<length).compactMap { _ in
letters.randomElement()
})
}
}

Resources