logo
down
shadow

NSCharacterSet.characterIsMember() with Swift's Character type


NSCharacterSet.characterIsMember() with Swift's Character type

By : John Sullivan
Date : November 17 2020, 11:58 AM
like below fixes the issue My understanding is that unichar is a typealias for UInt16. A unichar is just a number.
I think that the problem that you are facing is that a Character in Swift can be composed of more than one unicode "characters". Thus, it cannot be converted to a single unichar value because it may be composed of two unichars. You can decompose a Character into its individual unichar values by casting it to a string and using the utf16 property, like this:
code :
let c: Character = "a"
let s = String(c)
var codeUnits = [unichar]()
for codeUnit in s.utf16 {
    codeUnits.append(codeUnit)
}
let char: Character = "\u{63}\u{20dd}" // This is a 'c' inside of an enclosing circle
for codeUnit in String(char).utf16 {
    if NSCharacterSet(charactersInString: "c").characterIsMember(codeUnit) {
        dude.abide()
    } // dude will abide() for codeUnits[0] = "c", but not for codeUnits[1] = 0x20dd (the enclosing circle)
}
if NSCharacterSet(charactersInString: "c").characterIsMember(String(char).utf16[0]) {
    dude.abide()
}
func isChar(char: Character, inSet set: NSCharacterSet) -> Bool {
    return set.characterIsMember(String(char).utf16[0])
}

let xSet = NSCharacterSet(charactersInString: "x")
isChar("x", inSet: xSet)  // This returns true
isChar("y", inSet: xSet)  // This returns false
func isChar(char: Character, inSet set: NSCharacterSet) -> Bool {
    var found = true
    for ch in String(char).utf16 {
        if !set.characterIsMember(ch) { found = false }
    }
    return found
}

let acuteA: Character = "\u{e1}"                   // An "a" with an accent
let acuteAComposed: Character = "\u{61}\u{301}"    // Also an "a" with an accent

// A character set that includes both the composed and uncomposed unichar values
let charSet = NSCharacterSet(charactersInString: "\u{61}\u{301}\u{e1}")

isChar(acuteA, inSet: charSet)           // returns true
isChar(acuteAComposed, inSet: charSet)   // returns true (both unichar values were matched


Share : facebook icon twitter icon
NSCharacterSet - append another character set

NSCharacterSet - append another character set


By : I.Soder
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I would like to create a character set that includes all of its own characters, as well as those from another character set. Append in other words. , You are probably seaching for this method in NSMutableCharacterSet :
code :
- (void)formUnionWithCharacterSet:(NSCharacterSet *)otherSet
Why does my version of filter perform so differently than Swifts?

Why does my version of filter perform so differently than Swifts?


By : Satyajeet Singh
Date : March 29 2020, 07:55 AM
help you fix your problem Ok, so after reading all posted comments, I decided to also benchmark, and here are my results. Oddly enough, the built-in filter seems to perform worse than a custom implementation.
TL;DR; Because your function is short, and the compiler has access to it source code, the compiler inlines the function call, which enables more optimisations.
code :
import Darwin

extension Array {
    func myFilter(@noescape predicate: Element -> Bool) -> [Element] {
        var filteredArray = [Element]()
        for x in self where predicate(x) {
            filteredArray.append(x)
        }

        return filteredArray
    }
}

let arr = [Int](1...1000000)

var start = mach_absolute_time()
let _ = arr.filter{ $0 % 2 == 0}
var end = mach_absolute_time()
print("filter:         \(end-start)")

start = mach_absolute_time()
let _ = arr.myFilter{ $0 % 2 == 0}
end = mach_absolute_time()
print("myFilter:       \(end-start)")
filter:         370930078
myFilter:       479532958
filter:         15966626
myFilter:       4013645
extension Array {
    func originalFilter(
        @noescape includeElement: (Generator.Element) throws -> Bool
        ) rethrows -> [Generator.Element] {

            var result = ContiguousArray<Generator.Element>()

            var generator = generate()

            while let element = generator.next() {
                if try includeElement(element) {
                    result.append(element)
                }
            }

            return Array(result)
    }

}

start = mach_absolute_time()
let _ = arr.originalFilter{ $0 % 2 == 0}
end = mach_absolute_time()
print("originalFilter: \(end-start)")
filter:         13255199
myFilter:       3285821
originalFilter: 3309898
filter:         343038057
myFilter:       429109866
originalFilter: 345482809
filter: 11489238
myFilter: 6923719
myFilter not inlined: 9275967
my filter not inlined, with throws: 11956755
extension Array {
    @inline(never)
    func myFilter(predicate: Element throws -> Bool) rethrows -> [Element] {
        var filteredArray = [Element]()
        for x in self where try predicate(x) {
            filteredArray.append(x)
        }

        return filteredArray
    }
}
how to convert characterIsMember objective c to swift

how to convert characterIsMember objective c to swift


By : Kris Ray
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I have one part code objective c code and I want to convert to swift : , If you don't mind using NSString the port is straight forward:
code :
    let rawStr:NSString = tokenField.textField.text
    for i in 0..<rawStr.length {
        let currentChar = rawStr.characterAtIndex(i)
        if !charSet.characterIsMember(currentChar) {
            recipient.appendFormat("%@", NSString(format:"%c", currentChar))
        }
    }
    if rawStr.length > 0 {
        tokenField.addTokenWithTitle(rawStr, representedObject:recipient)
    }
Swift 3 conversion : value of type 'characterset' has no member 'characterIsMember'

Swift 3 conversion : value of type 'characterset' has no member 'characterIsMember'


By : iHatred OneTwo
Date : March 29 2020, 07:55 AM
this one helps. I am currently converting my codes to swift 3 and i encounter the above mentioned error with the following codes. , You can do as follow:
code :
extension String {
    var containsAlphabets: Bool {
       return utf16.contains { (CharacterSet.letters as NSCharacterSet).characterIsMember($0) }
    }
}
Is there an Android equivalent to iOS´s (swifts) fatalError()?

Is there an Android equivalent to iOS´s (swifts) fatalError()?


By : bon.julia
Date : March 29 2020, 07:55 AM
With these it helps I don't there's any method equivalent to fatalError in Android.
If you want to kill the entire process then you can use android.os.Process.killProcess(android.os.Process.myPid());.
Related Posts Related Posts :
  • consecutive statement need to have ; error in swift
  • Trying to create a 32 bpc NSBitmapImageRep, getting hit with errors
  • Does locationManager:didUpdateLocations: method called at the same time in extension and its containing app
  • Can't get Data using SwiftyJSON
  • Convert String to UnsafeMutablePointer<char_t> in Swift
  • inout param with empty object since nil won't work
  • How to align a mask onto an image
  • compiler forcing self inside static method of a function while using dispatch_sync
  • private static variable in struct
  • Conditional "in" for arrays in Swift
  • Extension for Double to accept String as initializer in Swift
  • Modifying UIButton Programmatically
  • Pass type to generic function and compare
  • How to customise the look of the PFSignUpViewController
  • How to take screen shot programmatically (Swift, SpriteKit)
  • Setting NavigationController's NavigationBar translucent property to false causes extra padding
  • Create multiple UIViews when the user taps the screen
  • Filter Array of [AnyObject] in Swift
  • How to add ParseUI to a swift project
  • Can Swift enums have multiple raw values?
  • Using a function() variable in a different Class - Swift
  • Masking an image in Swift using CALayer and UIImage
  • KVC without NSObject (NSPredicate)
  • Any? to Dictionary Cast and retrieving value
  • SCNScene: Calculate projected size of an object
  • How to use contains on a swiftyJSON object
  • Index of element in sorted()
  • How do I initialise a new NSDocument instance in Swift?
  • Multiple functions with the same name
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org