Can I put this in a loop instead of having to manually code it in for each chapter?
var chp1 = Array(Chapters[0].componentsSeparatedByString("\n"))
var chp2 = Array(Chapters[1].componentsSeparatedByString("\n"))
var chp3 = Array(Chapters[2].componentsSeparatedByString("\n"))
var chp4 = Array(Chapters[3].componentsSeparatedByString("\n"))
var chp5 = Array(Chapters[4].componentsSeparatedByString("\n"))
var chp6 = Array(Chapters[5].componentsSeparatedByString("\n"))
var chp7 = Array(Chapters[6].componentsSeparatedByString("\n"))
var chp8 = Array(Chapters[7].componentsSeparatedByString("\n"))
Yes you can use
var chp = []
for chapter in Chapters
{
chp.append(chapter.componentsSeparatedByString("\n")))
}
now you can access to the chapters like
chp[0]
chp[1]
chp[2]
Also you don't need to cast return value to Array.
for range in 0...8 {
var chp = Chapters[range]
let result = chp.componentsSeparatedByString("\n"))
}
Or
for (_, chapter) in Chapters.enumerate() {
let result = chapter.componentsSeparatedByString("\n"))
}
Related
Trying to get the formatting copied over from the front sheet, but it takes the forumla which is a import range, is there any way to achieve this without showing the ref error.
function BrandSheets() {
var ss = SpreadsheetApp.getActive();
var Frontsheet = ss.getSheets()[0];
var Header_range = Frontsheet.getRange(1,1,1,13);
var CurrentBrand = Frontsheet.getRange(2,3).getValue();
var CurrentTopBrand = 2;
var CurrentBottomBrand = 2;
var CheckBrand = CurrentBrand;
Frontsheet.getRange(1,3).activate();
Frontsheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.DOWN).activate();
var FLastCell = Frontsheet.getActiveCell();
var FLastRow = FLastCell.getRow();
for(var i = 2;i<=FLastRow+1;i++){
var CheckBrand = Frontsheet.getRange(i,3).getValue();
if (CheckBrand == CurrentBrand){
var CurrentBottomBrand = i
}
else{
var CurrentBottomBrand = i
var BrandTable = Frontsheet.getRange(CurrentTopBrand,1,CurrentBottomBrand-CurrentTopBrand,13);
var NewSheet = SpreadsheetApp.getActiveSpreadsheet().insertSheet(CurrentBrand);
var NewHeader_range = NewSheet.getRange(1,1,1,13);
var NewBrandTable = NewSheet.getRange(2,1,CurrentBottomBrand-CurrentTopBrand,13);
Header_range.copyTo(NewHeader_range,{contentsOnly:true});
BrandTable.copyTo(NewBrandTable);
var CurrentTopBrand = i;
var CurrentBrand = Frontsheet.getRange(i,3).getValue();
SpreadsheetApp.getActiveSpreadsheet().getSheets().forEach( sheet => sheet.setTabColor("grey"));
}
}
}
I tried Header_range.copyTo(NewHeader_range,{contentsOnly:true,formatOnly:true});
But this is where the bug of ref comes up. Issue Ref
Many thanks,
I have array of dictionaries that I will use for cells in collectionView. I'm new to swift, so I try to find the best way to store that.
Now I use this code:
var collectionViewData: [collectionViewStruct] = {
var cell_1 = collectionViewStruct()
cell_1.title = "..."
cell_1.text = "..."
cell_1.firstColor = "C68CF2"
cell_1.secondColor = "EFA8CA"
var cell_2 = collectionViewStruct()
cell_2.title = "..."
cell_2.text = "..."
cell_2.firstColor = "C68CF2"
cell_2.secondColor = "EFA8CA"
return [cell_1, cell_2]
}()
Is there a way not to write every var in return?
How to return all the variables at once?
Or maybe there is a better way to store this data?
Thanks in Advance :)
If the suggestions data is never changing, just use the following struct:
struct collectinView {
let title: String
let text: String
let firstColor = "C68CF2"
let secondColor = "EFA8CA"
}
let collectionViewData = [
collectionView(title: "...", text: "..."),
collectionView(title: "other Title", text: "Other text")]
For achieving this you can use a private property and return that instead like:
private var _suggestions: [suggestionsStruct] = [suggestionsStruct]()
var suggestions: [suggestionsStruct] {
get{
if _suggestions.count > 0{
var suggestion_1 = suggestionsStruct()
suggestion_1.title = "..."
suggestion_1.text = "..."
suggestion_1.firstColor = "C68CF2"
suggestion_1.secondColor = "EFA8CA"
_suggestions.append(suggestion_1)
}else{
var suggestion_1 = suggestionsStruct()
suggestion_1.title = "..."
suggestion_1.text = "..."
suggestion_1.firstColor = "C68CF2"
suggestion_1.secondColor = "EFA8CA"
_suggestions = [suggestion_1]
}
return _suggestions
}
}
I have a array that looks like this:
var myArray = ["1one", "1two", "1three", "1four", "1five", "1six"]
And to get random, I use this:
var originalNames = [String]()
func getRandomName() -> String {
if (names.count == 0) {
names = originalNames
}
let randomNumber = Int(arc4random_uniform(UInt32(names.count)))
return names.removeAtIndex(randomNumber)
}
And I use it like this:
self.randomLabel.text = getRandomName()
As you can see, the array contains six different strings. The code that I am currently using, will return add the strings inside the array at random, but I only want to return the first 3 strings randomly. How can I do this?
You can try using
var originalNames = [String]()
func getRandomName() -> String {
if (names.count == 0) {
names = originalNames
}
let randomNumber = Int(arc4random_uniform(3))
return names[randomNumber]
}
so let randomNumber = Int(arc4random_uniform(3)) this will return random Int value upto 3.
var myArray = ["1one", "1two", "1three", "1four", "1five", "1six"]
var result:[String] = []
while result.count < 3 {
let randomNumber = Int(arc4random_uniform(UInt32(myArray.count)))
result.append(myArray.removeAtIndex(randomNumber))
}
print(result) // "["1two", "1one", "1three"]\n"
if you don't want to modify the original array just make a copy of it
let myArray = ["1one", "1two", "1three", "1four", "1five", "1six"]
var inputNames = myArray
var result:[String] = []
while result.count < 3 {
result.append(inputNames.removeAtIndex(Int(arc4random_uniform(UInt32(inputNames.count)))))
}
print(result) // "["1six", "1two", "1one"]\n"
When I try to append a list which contain list of image which I parsed from JSON into an array in Swift iOS I mm getting an error:
"Cannot invoke append with an argument list of type json"
I'm totally new to iOS (Swift).
This is the basic code I'm using Alamofire, SwiftyJSON and SwiftyUserDefaults.
import Alamofire
import SwiftyJSON
import SwiftyUserDefaults
var NameArr: [String] = []
var ProfileImgArr: [String] = []
var StyleTypeArr: [String] = []
var ImageStackArr = [String]()
var SingleImageArr: [String] = []
var ItemNameArr: [String] = []
var TagsArr: [String] = []
var ImageArrSet: [String] = []
var LikeCountArr: [Int] = []
var ImageFlag: [String] = []
var testCount: (Int) = 0
var imageCounter = 0
var normalCounter = 0
var normalCounter1 = 0
var normalCounter2 = 0
// JSON load
let url = "http://zapmob.cloudapp.net/media/"
func JsonLoad() {
Alamofire.request(.GET, "http://zapmob.cloudapp.net/feed/?format=json")
.responseJSON { (request, response, data, error) in
println(error)
println(response)
let json = JSON(data!)
for (var j = 0; j <= json.count-1; j++) {
let FullName = json[j]["user_details_dict"]["username"].string
self.NameArr.append(FullName!)
let ProfilePic = json[j]["user_details_dict"]["profile_pic"].string
self.ProfileImgArr.append(ProfilePic!)
let StyleType = json[j]["fashion_type"].string
self.StyleTypeArr.append(StyleType!)
// -- Start of error part --v
var ImageStackArrs = [json[j]["images"].array!]
println(ImageStackArrs)
Defaults["ImageArrStack"] = ImageStackArrs
for (var x = 0; x <= ImageStackArrs.count - 1; x++) {
Defaults["ImageStackCache"] = [ImageStackArrs[x][0]]
}
// -- End of error part --^
let ItemName = json[j]["album_title"].string
self.ItemNameArr.append(ItemName!)
let LikeCount = json[j]["like"].count
self.LikeCountArr.append(LikeCount)
}
Defaults["NameCache"] = self.NameArr
Defaults["ProfileImgCache"] = self.ProfileImgArr
Defaults["StyleTypeCache"] = self.StyleTypeArr
Defaults["ItemNameCache"] = self.ItemNameArr
Defaults["TagsCache"] = self.TagsArr
Defaults["LikeCountCache"] = self.LikeCountArr
}
}
Without going through the code in detail: This error is normally called when trying to append to an optional array.
Im trying to make a function that takes in an Array of Arrays that returns a single Array so that all values ("names") have there own index.
var namesList1 = [String]()
var namesList2 = [String]()
var namesList3 = [String]()
namesList1 = ["Paul","John","Ringo","George"]
namesList2 = ["Julie","Sarah","Jackie"]
namesList3 = ["Jim","Jack","Charlie","Sally","Debra"]
var namesCombinedArray = [NSArray]()
namesCombinedArray = [namesList1,namesList2,namesList3]
func total(arrays:NSArray) -> NSArray{
// how to loop to create an array with index(s) to all names ??
var completeList = [NSArray]()
return completeList
}
You could use flatMap:
let namesList1 = ["Paul","John","Ringo","George"]
let namesList2 = ["Julie","Sarah","Jackie"]
let namesList3 = ["Jim","Jack","Charlie","Sally","Debra"]
let namesCombinedArray = [namesList1,namesList2,namesList3]
let completeList = namesCombinedArray.flatMap{$0}
print(completeList)
// [Paul, John, Ringo, George, Julie, Sarah, Jackie, Jim, Jack, Charlie, Sally, Debra]