I have this code in swift2
private var RecordedDurationContext = UnsafeMutablePointer<Void>.alloc(1)
How convert into swift3
private var RecordedDurationContext = UnsafeMutableRawPointer.alloc(1)
let a = UnsafeMutablePointer<Int>.allocate(capacity: 1)
a.pointee = 42
print("a's value: \(a.pointee)") // 42
a.deallocate(capacity: 1)
Related
I'm using swiftjson and alamofire to parse the json.pls help fix my code.
here the json array
Below are my code
RestClient.sharedClient.getUserWallet(uid: uid!, token: token!) { (json, error) in
print(json as Any)
self.tableRows = json!["data"].count
//ALL BELOW IS DONE BY SWIFTYJSON
let jsonArray = json?["data"].array
if jsonArray != nil {
for object in jsonArray! {
let walletCells: UserWalletCells = UserWalletCells(json: object)
self.userWalletCells.append(walletCells)
}
}
class PackageList {
var id: Int?
var user_id: String?
var amount: Int?
var currency_id: Int?
var release_rate: Double?
var release_per_day: Int?
var release_date: String?
var end_date: String?
var total_release: Int?
var tier_id: Int?
var is_active: Int?
var created: String?
var modified: String?
var remainingdays: Int?
init(json: JSON){
self.id = json["id"].intValue
self.user_id = json["user_id"].stringValue
self.amount = json["amount"].intValue
self.currency_id = json["currency_id"].intValue
self.release_rate = json["release_rate"].doubleValue
self.release_per_day = json["release_per_day"].intValue
self.release_date = json["release_date"].stringValue
self.end_date = json["end_date"].stringValue
self.total_release = json["total_release"].intValue
self.tier_id = json["tier_id"].intValue
self.is_active = json["is_active"].intValue
self.created = json["created"].stringValue
self.modified = json["modified"].stringValue
self.remainingdays = json["remainingdays"].intValue
}
}
I'm using swiftjson and alamofire to parse the json.pls help fix my code.
Please try this it will help you
let json = JSON(response)
let dictResult = json["data"].dictionaryValue
let arrayValue = dictResult["apckage"].arrayValue
var arrList = [PackageList]()
for i in 0..<arrayValue.count{
let packageList = PackageList()
let dictRespose = arrayValue[i]
packageList. id = dictRespose["id"].stringValue
packageList. user_id = dictRespose["user_id"].stringValue
packageList. amount = dictRespose["amount"].stringValue
arrList.append(packageList)
}
return arrTaxLis
Like this you put all your key. It may helps you. Thank you.
I'm Trying reorder UICollectionViewcell Images on drag and drop using RealmSwift As database, My UI is not updating on a drag and drop and strange behaviour, some Images are duplicating , my code is Like this
RealmModel As
class StoryAlbumDM: Object {
dynamic var id = 0
dynamic var type = ""
dynamic var isImage: Int = 0
dynamic var textData = ""
dynamic var imageData: NSData? = nil
dynamic var rowId: Int = 0
dynamic var position: Int = 0
dynamic var storyId: Int = 0
dynamic var isCoverImage: Int = 0
dynamic var imagePath = ""
let allStories = List<StoryAlbumDM>()
}
On drag and drop I'm doing Like this
func collectionView(collectionView: UICollectionView, atIndexPath: NSIndexPath, didMoveToIndexPath toIndexPath: NSIndexPath) {
print("moveItemAtIndexPath")
let fromIndexPath: Int = atIndexPath.row
print("from", fromIndexPath)
let toIndexPathInt: Int = toIndexPath.row
print("To", toIndexPath)
let fromData: StoryAlbumDM!
fromData = realm.objects(StoryAlbumDM.self).filter("position = %d AND storyId = %d", fromIndexPath, self.storyID).first!
let toData: StoryAlbumDM!
toData = realm.objects(StoryAlbumDM.self).filter("position = %d AND storyId = %d", toIndexPath, self.storyID).first!
var tempData = StoryAlbumDM()
self.performSelectorOnMainThread(#selector(StoryViewController.updateSrtoryInRealm), withObject: self.collectionView, waitUntilDone: true)
dispatch_async(dispatch_get_main_queue(), {
self.collectionView.performBatchUpdates({
self.collectionView.reloadData()
}, completion: nil)
})
}
func updateSrtoryInRealm() {
self.tempData.type = self.toData.type
self.tempData.isImage = self.toData.isImage
self.tempData.textData = self.toData.textData
self.tempData.rowId = self.toData.rowId
self.tempData.imageData = self.toData.imageData
self.tempData.position = self.toData.position
self.tempData.storyId = self.toData.storyId
self.tempData.isCoverImage = self.toData.isCoverImage
self.tempData.imagePath = self.toData.imagePath
do {
try! realm.write {
self.toData.type = self.fromData.type
self.toData.isImage = self.fromData.isImage
self.toData.textData = self.fromData.textData
self.toData.rowId = self.fromData.rowId
self.toData.imageData = self.fromData.imageData
self.toData.position = self.fromData.position
self.toData.storyId = self.fromData.storyId
self.toData.isCoverImage = self.fromData.isCoverImage
self.toData.imagePath = self.fromData.imagePath
// title.id = temp.id
self.fromData.type = self.tempData.type
self.fromData.isImage = self.tempData.isImage
self.fromData.textData = self.tempData.textData
self.fromData.rowId = self.tempData.rowId
self.fromData.imageData = self.tempData.imageData
self.fromData.position = self.tempData.position
self.fromData.storyId = self.tempData.storyId
self.fromData.isCoverImage = self.tempData.isCoverImage
self.fromData.imagePath = self.tempData.imagePath
}
//}
}
catch {
print("Printed error : ")
}
Problem: Images Are duplicating, Not updating on UI , Reorder strange behaviour, please help me on this
I answered a similar question recently, but I'll re-explain it here. :)
Easily, the best and quickest way to re-order Realm objects inside a Realm file is to make an overarching List object that holds all of the Realm objects of a given type.
For example in this case, you make another object to hold that allStories value you already created:
// Model class that manages the ordering of story album objects
class StoryAlbumDMList: Object {
let allStories = List<StoryAlbumDM>()
}
// Model class for the actual story album objects
class StoryAlbumDM: Object {
dynamic var id = 0
dynamic var type = ""
dynamic var isImage: Int = 0
dynamic var textData = ""
dynamic var imageData: NSData? = nil
dynamic var rowId: Int = 0
dynamic var position: Int = 0
dynamic var storyId: Int = 0
dynamic var isCoverImage: Int = 0
dynamic var imagePath = ""
}
This way, when you want to re-order the list, all you need to do is re-order them inside this array.
Like I said in the other question, one other way you can do it (Which is not as good, but also doesn't require an extra Realm object) is to add another property named orderedIndex, which simply contains a number indicating the numerical order of these objects. When you want to re-order them, it's simply a matter of re-setting these numbers.
Let me know if you need any more clarification!
I know that question has been asked many times, but the solutions didn't work with me. I have the following NSManaged object class:
#NSManaged var cellColor: AnyObject
#NSManaged var des: String
#NSManaged var name: String
#NSManaged var switcher: NSNumber
And when trying to assign this value to a var as a bool when loading the managed object, I get an error. This is an example of assigning the value to a var:
func loadData(){
var appDel = UIApplication.sharedApplication().delegate as AppDelegate
var context = appDel.managedObjectContext!
var request = NSFetchRequest(entityName: "Row")
var result:NSArray = context.executeFetchRequest(request, error: nil)!
if result.count > 0 {
for i in result{
var name = i.name as String
var des = i.des as String
var color = i.cellColor as UIColor
var switcher:Bool{
get{
return i.switcher == NSNumber(bool: true)
//the problem happens here
However, when I try a simple example of casting in this way, It works well.
How to solve that ??
I think it just like this:
for i in results {
var name = i.name as String
var des = i.des as String
var color = i.cellColor as UIColor
var switcher: Bool {
return Bool(i.switcher)
}
}
Didn't test that though.
I am new to programming with swift and am confused as to why the program does not recognise what I have labeled as "test", claiming it is unresolved.
import UIKit
var rainfall = [38,94,142,149,236,305,202,82,139,222,178,103]
var raindays = [3,6,8,7,12,16,10,8,12,14,11,7]
let crit_raindays = 11
let et_raindays_lessthan_11 = 150
let et_raindays_morethan_11 = 120
let max_h2Ostore = 150
var evap_transpiration: [Int] = []
for i in raindays {
if i <= 11 {
var test = et_raindays_lessthan_11
}
else if i > 11 {
var test = et_raindays_morethan_11
}
evap_transpiration.append(test)
}
The variable test does not seem to be assigned properly, I have no idea why.
Error message: Use of unresolved identifier "test"
When you declare a local variable in Swift, it will be accessible only after its declaration and in the the same scope. In your sample code: the first declaration will be accessible only inside the first if statement, and the second declaration will be accessible only inside the second one.
The correct way is:
for i in raindays {
var test = 0
if i <= 11 {
test = et_raindays_lessthan_11
}
else if i > 11 {
test = et_raindays_morethan_11
}
evap_transpiration.append(test)
}
Even more, you don't need the second if, since if the first one is false, the second one will be always true. So, the code is:
for i in raindays {
var test = 0
if i <= 11 {
test = et_raindays_lessthan_11
}
else {
test = et_raindays_morethan_11
}
evap_transpiration.append(test)
}
Try this:
var test: Int
for i in raindays {
if i <= 11 {
test = et_raindays_lessthan_11
}
else if i > 11 {
test = et_raindays_morethan_11
}
evap_transpiration.append(test)
}
The reason is that you define test inside a block, so the scope is only internal to that block.
You need to declare test outside the for loop. Here is what I have that works.
import UIKit
var rainfall = [38,94,142,149,236,305,202,82,139,222,178,103]
var raindays = [3,6,8,7,12,16,10,8,12,14,11,7]
let crit_raindays = 11
let et_raindays_lessthan_11 = 150
let et_raindays_morethan_11 = 120
let max_h2Ostore = 150
var evap_transpiration: [Int] = []
var test = 0
for i in raindays {
if i <= 11 {
test = et_raindays_lessthan_11
}
else if i > 11 {
test = et_raindays_morethan_11
}
evap_transpiration.append(test)
}
I'm building a swift game and a need to set up a class. My code works for all the elements in my class, but not for this.
func saveInformationMember(){
var MembersDefaultName = NSUserDefaults.standardUserDefaults()
MembersDefaultName.setValue(globalCurrentMembers, forKey: "globalCurrentMembersData")
MembersDefaultName.synchronize()
}
GlobalCurrentMembers is an array of Member which looks like that:
class Member {
var image = String ()
var name = String ()
var progression = Int()
var round = Int()
var level = Int()
var imageProgression = [UIButton]()
func Init(){
image = "default.png"
name = "default"
progression = 0
round = 0
level = 0
}
To save your class this way, Member needs to conform to the NSCoding protocol.
Thx to Aaron Brager for is response. This is the response :
func saveInformationMember(){
let data = NSKeyedArchiver.archivedDataWithRootObject(globalCurrentMembers)
NSUserDefaults.standardUserDefaults().setObject(data, forKey: "member")}
func loadInformationMember(){
if let data = NSUserDefaults.standardUserDefaults().objectForKey("member") as? NSData {
globalCurrentMembers = NSKeyedUnarchiver.unarchiveObjectWithData(data) as [Member]
}
And my class:
class Member : NSObject, NSCoding {
var image = String ()
var name = String ()
var progression = Int()
var round = Int()
var level = Int()
var imageProgression = [UIButton]()
func initiation(){
image = "default.png"
name = "default"
progression = 0
round = 0
level = 0
}
required convenience init(coder decoder: NSCoder) {
self.init()
self.image = decoder.decodeObjectForKey("image") as String!
self.name = decoder.decodeObjectForKey("name") as String!
self.progression = decoder.decodeIntegerForKey("progression") as Int!
self.round = decoder.decodeIntegerForKey("round") as Int!
self.level = decoder.decodeIntegerForKey("level") as Int!
}
func encodeWithCoder(coder: NSCoder) {
coder.encodeObject(self.image, forKey: "image")
coder.encodeObject(self.name, forKey: "name")
coder.encodeInt(Int32(self.progression), forKey: "progression")
coder.encodeInt(Int32(self.round), forKey: "round")
coder.encodeInt(Int32(self.level), forKey: "level")
}}