NSMutableArray to Array of String Conversion Swift iOS [duplicate] - ios

This question already has answers here:
How can I cast an NSMutableArray to a Swift array of a specific type?
(7 answers)
Closed 7 years ago.
I need to populate data in a table view. How can I convert [NSMutableArray?] to [(String)] in Swift iOS
println(self.subcategory[index]!) i.e., an [NSMutableArray?], where
index is Int shows:
(
Operations,
Marketing,
"Public Relations",
"Human Resources",
Advertising,
Finance,
Hotels,
Restaurants,
Other
)

Try with this.
let myArray : NSMutableArray = ["Operations"]
myArray.addObject("Marketing")
myArray.addObject("Public Relations")
myArray.addObject("Human Resources")
myArray.addObject("Advertising")
myArray.addObject("Finance")
myArray.addObject("Hotels")
myArray.addObject("Restaurants")
myArray.addObject("Other")
NSLog("\(myArray)")
var string = myArray.componentsJoinedByString(",")
NSLog("\(string)")

Related

Xcode: XCTest cannot no compare two NSArray of numbers [duplicate]

This question already has answers here:
XCTAssertEqual fails to compare two string values?
(3 answers)
Closed 2 years ago.
I'm trying to implement a unit test(XCTest) to compare two NSArray objects:
Here is my implementation:
- (void)testTwoNumsArrays {
NSArray *result = #[#20,#20];
NSArray *expecteResult = #[#20,#20];
XCTAssertEqual(result, expecteResult);
}
I'm getting this error:
((result) equal to (expecteResult)) failed: ("{length = 8, bytes = 0xb0cfc00001000000}") is not equal to ("{length = 8, bytes = 0x9032ce0001000000}")
Any of you know why I can not compare the two arrays. In Swift it works just fine.
I'll really appreciate your help.
The problem is you are comparing the pointers to the objects and not the objects themselves.
XCTAssertEqual is essentially doing a check like the following, which is only comparing the pointer values.
if (a != b) assert();
You want something more along the lines of
if (![a isEqual:b]) assert();
This can be achieved with the XCTAssertEqualObjects call.
More information:
XCTAssertEqual fails to compare two string values?
https://developer.apple.com/documentation/xctest/xctassertequalobjects

country picker swift 4 [duplicate]

This question already has answers here:
Getting country name from country code
(8 answers)
Closed 4 years ago.
I would like implement a native iOS country picker in swift 4.
Currently I have a table which contain the countries code:
var countriesCodes = NSLocale.isoCountryCodes as [String]
I implement the delegate, and I have a problem in the data source.
How to convert each country code in country name?
You can convert each country code key into its localised display name using the following function provided in Foundation (documentation here):
Locale.current.localizedString(forRegionCode:)
// "NZ" -> "New Zealand"
You can convert your country code array to display names using flatMap:
let countryNames = countriesCodes.flatMap(
Locale.current.localizedString(forRegionCode:))
Although you may wish to make a tuple pair of the ISO country code and its localised display name together, which would be much more useful:
let countryCodesAndNames = countriesCodes.flatMap { code in
Locale.current.localizedString(forRegionCode: code).map { (code, $0) }
}
// countryCodesAndNames is type [(String, String)]
// which is (ISO code, displayName)
// eg.
print(countryCodesAndNames[0])
// prints ("AC", "Ascension Island")

Filtering with NSPredicate an Array of Array to see if It contains an Integer in Swift [duplicate]

This question already has answers here:
Using NSPredicate to filter array of arrays
(2 answers)
Closed 5 years ago.
Hi I am trying to learn a bit more about NSPredicate- and its quite tough as the resources are still based on ObjC. I would like a swift based answer if possible
Basically I am trying to use NSPredicate to see if the arrays below contain a "9" the code number for Male users.
I thus want to filter the below people array and only keep arrays that contain the number 9.
var malePerson1:[Int] = [1,4,5,6,11,9]
var malePerson2:[Int] = [3,5,6,7,12,9]
var femalePerson3:[Int] = [3,5,6,7,12,10]
var people = [malePerson1, malePerson2, femalePerson3]
I have got the solution working fine using a filter (see below)
//Solution working with Filter
// male gender search
var result = people.filter{$0.contains(9)}
print(result)
var resultFemale = people.filter{$0.contains(10)}
print(resultFemale)
but how Do I do something similar using predicates?
Below doesn't work - resultMale just returns a blank array when it should contain the two arrays: [maleperson1, maleperson2]. I think the problem is that its checking if 9 is contained in the 'people' array instead of checking the contents of the contained arrays.
Any ideas how to do what I am doing using NSPredicate to filter the integers in the array?
let malePred = NSPredicate(format: "9 IN %#",people)
//9 is the code for Male user
var resultMale = (people as NSArray).filtered(using: malePred)
You can use ANY to compare each nested array with NSPredicate.
let malePred = NSPredicate(format: "ANY self == 9")

ios - filter two arrays with objects Swift [duplicate]

This question already has answers here:
How to remove common items from two struct arrays in Swift
(3 answers)
Closed 6 years ago.
I have got two arrays with objects.
var filteredData:[MainData] = [MainData]()
var removeData:[MainData] = [MainData]()
struct MainData {
var open:NSTimeInterval
var works = [Visit]()
}
I want remove data from filteredData using function filter with parameter filteredData.open == removeData.open
I can't filter two arrays with objects.
You can try like this, first get an Array of open from removeData array and check that it is contains object from the filteredData Array opens.
let opens = removeData.map { $0.open }
filteredData = filteredData.filter { !opens.contains($0.open) }

How to split a string (such as "xPM - yPM") into two strings, Swift [duplicate]

This question already has answers here:
Split a String into an array in Swift?
(40 answers)
Closed 7 years ago.
If I have a string such as xAM - yPM is it possible to split the string into 2 strings using the - as the point where the string is split?
You can use the componentsSeparatedByString function:
var splittedArray = yourString.componentsSeparatedByString(" - ")
println(splittedArray[0]) // "xPM"
println(splittedArray[1]) // "yPM"

Resources