Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm having an array of data, i need to call a API Function and use my array value. Each time the API is hitting respected value should be deleted and my function should be called until the array becomes empty.
Try this example.
call this method process first time and pass your array like
process(array)
it will call itself until array contains element/s.
let array = [1,2,3]
func process(_ array: [Int]){
if array.isEmpty{ return }
let element = array.first!
//process element
print(element)
let newArray = Array(array.dropFirst())
process(newArray)
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
When the app runs it crashed and write the text field is nil and the images are nil and write you must warp its and didn't open the view controller which has this images and text field
Name = (UITextField!) nil
phone = (UITextField!) nil
url = (UITextField!) nil
Name = (UIImageView!) nil
If you have created the UITextField and UIImageViews objects using nib or in storyboard, check whether the IBOutlets of them are connected.
If you have created these UI elements via code(in a function), check the function is getting called.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am trying figure out what would be the best option to save some info into cache. I want it to be in there until notification pops out and button is pressed. I am already done everything else but I do not know what is the best solution for handling temporary values in cache. I need to use them even if app is closed. Is CoreData the best solution with NSUserDefaults? Lets assume that I have an array: var myArray = []
and I have textField. Now I want that texField input to be in array and if notification category button is pressed it deletes the first array item from the myArray. I need all this even if app is closed. Faulty code is inside "**".
Edit: The main problem is that how can delete every time the last value from array? It is not working if app is closed but works if it is opened.
This is what i've done so far.
This is whre I call out notification and it's action:
This is my array: var myArray = [String]()
Here I add object into array:
And now if notification is fired and button is pressed I would like to fire this function to delete first object from array:
You can use NSUserDefaults. They are accessable if the app is closed or open.
Read more about NSUserDefaults Here
Saving Example:
NSUserDefaults.standardUserDefaults().setObject(nameField.text!, forKey: "name")
Retrieving Example
Best Practice
if let predicate = NSUserDefaults.standardUserDefaults().objectForKey("name") {
//NSUserDefaults has a value
} else {
//NSUserDefaults does not has a value
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
In my application I created a structure with two properties:
struct questionCollection {
var question: String!
var answer: String!
}
I then created an instance of that:
questionCollections = [questionCollection(question: "What is 1+1?", answer: "2")]
Along with this I have a function that allows the user to add a question:
#IBAction func appendQuestion(sender: AnyObject) {
questionCollections += [questionCollection(question: questionInput.text, answer: answerInput.text)]
}
My question is what is the easiest way to save the user submitted data? I have only used NSUserDefaults before and I don't know what to do.
I really couldn't find any solution to my problem so if I could get some help that would be great. Thanks
To clarify: All I want to do is have the data stored so can be shown on a label and check to see if the correct answer is chosen. The data does not need to be seen by the user until it comes up in the label. I am also just creating a simple app so I only need this data to be saved on the device.
With what I am doing now I am not sure if I need to save my data but if someone can still answer this if they want for future reference.
Have you ever tried using Core Data before? It is Apple's default solution for persistent storage (saving things locally and having them appear again on every load of the app) . More information can be found here. https://developer.apple.com/library/watchos/documentation/Cocoa/Conceptual/CoreData/index.html
Currently, your structure can be represented as a dictionary value:
let dictionary:[String:String] = ["What is 1+1":"2", "What is 2+2": "4"]
and you can store a dictionary inside NSUserDefaults:
NSUserDefaults.standardUserDefaults().setObject(dictionary, forKey: "aKey")
I would say, this would be optimal solution in you case ...
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to reset the state of this statement:
[[IAPManager sharedInstance]isProductPurchased:kInAppItem] = FALSE;
However, i'm getting the error of "Assigning to 'readonly' return result of an Objective-C message not allowed". How do i reset the state of this statement?
In IAPManager.m
-(BOOL)isProductPurchased:(NSString *)productIdentifier
{
return [_purchasedProducts containsObject:productIdentifier];
}
Your left hand side is a method invocation that returns a BOOL, it is not a reference to the property itself, so you cannot assign a value.
You need to refer to the code for IAPPurchase, but if it this library then there is no method to reset the purchased state for a product.
You can delete the app or delete the plist the library uses to store purchase data (by the way, this isn't a very secure way of recording in-app purchases)
You can't assign a value to a return value of a method in Objective-C. You may be confused by the syntax
myObject.myProperty = myValue;
This sets the value of myProperty to myValue, but in doing so it essentially calls this method:
[myObject setMyProperty:myValue];
Either way, the left side of your code will be handled as a return value, not a property. Therefore you can't assign it.
If there exists a method like setProductPurchased: or setIsProductPurchased:, you need to call that like the second example. Otherwise there isn't a way to set the property, so you may have to set an instance variable directly.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
In a for loop,
NSLog (#"%#", [[<array> objectAtIndex:i]-><specific_var>];
Each index position should have two vars and I'd like to access one of them. Thanks much.
Edited: in this case I'm trying to access a property
You're far better off creating a temporary local variable. It will help you get access to the property without using -> and make your code clearer.
E.g.
TheObject *object = array[i]; //same as [array objectAtIndex:i]
NSLog(#"%#", object.property);
The only time we apply -> to an Objective-C object (other than self) is when we want to access the object's instance variables from outside of a method on that object. And we almost never want to do that.
Anyway, the return type of the objectAtIndex: method is id. An id has no public instance variables.
If you know the objects in your array are of a specific type, and you want to access the instance variables of those objects directly (which is generally strongly frowned upon), you must cast the return value to the correct type first. Example:
NSLog(#"%#", ((MyObject *)[myArray objectAtIndex:i])->_myInstanceVariable);
But generally you are much better off using accessor methods, which usually don't require you to cast from id to a more specific type.
More easily using the literal syntax:
NSLog(#"%#", [someArray[i] someGetter]);
or is it is an array of arrays:
NSLog(#"%#", someArray[i][someIndex]);