In Swift 2, I had used the following code:
for (_, value) in self.frc.fetchedObjects!.enumerated() {
if (value.value(forKeyPath: "name_of_field_of_entity_name") as? String == "S") {
...
}
}
Now, using Xcode 8.0 beta (8S128d) and Swift 3, Xcode tell me:
Value of type 'NSFetchRequestResult' has no member 'value'
How can fix this error?
Thank you
I solved on different way.
Instead of using loop with enumerated(), I created an array of objects and I access to XXX attribute by:
let xxx = self.frc.fetchedObjects as! [XXX]
for i in 0..<(xxx.count) {
print(xxx[i].attribute_of_XXX)
}
Related
I have an interface that looks like that:
interface Person {
name: string;
age: number;
group: Options
}
and an enum that looks like this:
export enum Options {
ONE = 'one',
TWO = 'two',
THREE = 'three',
}
I want to create a new object where the key will be one of the Options and the value will be Person.
I tried something like that:
type PersonMapped = {
[key in Options]: Person; // error on the in keyword
}
But I get an error that says: parsing error: Unexpected token, expected "]".
What's wrong with this code?
Edit 1:
Here's the code example that I try to use:
export enum Group {
ONE = 1,
TWO = 2,
THREE = 3
}
export type Mappable = {
[key in Group]: string
}
Here's the error from the ide:
Edit 2:
It seems that the problem is with my editor settings. I'm using vscode version - 1.59.1. I also have eslint extension installed with version - 2.1.23.
I'm working on a groovy method to look for a custom attribute and return the value if the key is found.
The problem is that the method is returning the type of value instead of the value.
// There is more code before, but its not involved with this issue.
def UUIDca = 'UUID'
String customAttributeValue = grabCustomAttribute(UUIDca, event_work)
appendLogfile("\n\nTest grabCustomAttribute: ${customAttributeValue}\n")
}
// Grab the Custom Message Attribute values by name
String grabCustomAttribute (String findElement, OprEvent event){
appendLogfile("""\nIN grabCustomAttribute\nElement to look: ${findElement}\n""")
def firstItem = true
if (event.customAttributes?.customAttributes?.size()) {
event.customAttributes.customAttributes.each { ca ->
// Define each CMA to catch here
appendLogfile("""\nElement: ${ca.name} - """)
appendLogfile("""Valor: ${ca.value}\n""")
if ("${ca.name}" == findElement) {
String customValue = ca.value
appendLogfile("""Custom Attribute Found\n""")
appendLogfile(customValue)
return customValue
}
}
}
}
appendLogfile is basically a print to a log file :)
This is the output I'm getting.
IN grabCustomAttribute Element to look: UUID
Element: UUID - Valor: c3bb9169-0ca3-4bcf-beb1-f94eda8ebf1a
Custom Attribute Found
c3bb9169-0ca3-4bcf-beb1-f94eda8ebf1a
Test grabCustomAttribute: [com.hp.opr.api.ws.model.event.OprCustomAttribute#940e503a]
Instead of returning the value, it returns the type of object. It's correct, but I'm looking for the value.
I believe the solution is really simple, but I'm very new to Groovy.
Any help will be appreciated.
Thanks.
In this case the return statement is for the closure, not for the method, so your method is actually returning the list that "each" is iterating over
The easiest approach you can take here is to use Groovy find method to find the element you are searching for. Something like this:
String grabCustomAttribute (String findElement, OprEvent event) {
return event.customAttributes.customAttributes?.find { ca -> ca.name == findElement }.value
}
How come I cannot declare an array of People in Groovy as shown.
Maybe I'm lacking the deeper understanding of classes
class People {
Integer id
}
class Job {
def func() {
People[] p = new People[10]
}
}
I get an error of People[] cannot be applied to app.People[]
The code sample you have shown does not reproduce the error you mentioned in the question above. It's broken actually and does not compile - method func() is missing its body. If you correct the code to e.g.
class People {
Integer id
}
class Job {
def func() {
People[] p = new People[10]
assert p.size() == 10
println p
}
}
new Job().func()
you will see it produces the expected result - check it out in the Groovy web console here. When you run it you will see following output to the console:
[null, null, null, null, null, null, null, null, null, null]
The difference between Groovy and Java
When it comes to array initialization there is one significant difference between Groovy and Java. In Java you can initialize an array of People[] like this:
People[] p = new People[] { new People(), new People(), /* ... */ new People() };
It wont work in Groovy, because Groovy reserves {} for closures. In Groovy you can initialize such array as:
People[] p = [new People(), new People(), new People()] as People[]
While Szymon Stepniak's answer is correct for Groovy 2.5 and below, Java-style array initialization are part of the enhancements of Groovy 3.0 and 2.6 made possible by the new parrot parser.
Example from the release notes:
def primes = new int[] {2, 3, 5, 7, 11}
assert primes.size() == 5 && primes.sum() == 28
assert primes.class.name == '[I'
def pets = new String[] {'cat', 'dog'}
assert pets.size() == 2 && pets.sum() == 'catdog'
assert pets.class.name == '[Ljava.lang.String;'
// traditional Groovy alternative still supported
String[] groovyBooks = [ 'Groovy in Action', 'Making Java Groovy' ]
assert groovyBooks.every{ it.contains('Groovy') }
Szymon Stepniak's answer is correct. I'll point another example of a real case in some unit test that I've worked (general Object type):
Object[] o = [YourModel] as Object[]
This is sufficient to mock a general Object with your model properties.
When I extend a module and write code like below the plp variable is null, any idea why?
If I replace by PDP instead of PLP, I get value in variable.
return {
mountToApp: function mountToApp (container)
{
var plp = container.getComponent('PLP');
if(plp){
plp.addChildViews(
PLP.PLP_VIEW,
{
'GlobalViews.StarRating': {
'GlobalViews.StarRating': {
childViewIndex: 10
, childViewConstructor: function ()
{
return new FacetsExtension({
categoryInternalId: FacetsModel.get('category')? FacetsModel.get('category').get('internalid'):""
, application: this.application
});
}
}
}
}
);
What SuiteCommerce Version you are using? Please take note that PLP component is not available from killimanjaro and below versions. It only works on that latest version right now which is Aconcagua.
I have a problem that I can not understand.
There is a for-each loop, and the debugger tells me there are values in the list, but the concrete body-loop says the current element is null.
How can that be?
public void test(){
List cs = ["a"];
for(String c:cs){
print c; // but c is null(sais the debugger)! The console shows "null".
}
}
Edit: Another occourence is:
List<StaticSubFoobary> getBackendSubFoobaryList(List<String> electedSubFoobaryIds) {
List<StaticSubFoobary> subFoobaries = getStaticBackendFoobaryList()?.collect { StaticMainFoobary cat -> cat.backendSortedSubFoobaries }?.flatten()
List<StaticSubFoobary> electedSubFoobaries = subFoobaries.findAll { it.numericId in electedSubFoobaryIds}
return electedSubFoobaries
}
Do throw an NPE
But:
List<StaticSubFoobary> getBackendSubFoobaryList(List<String> electedSubFoobaryIds) {
List<StaticSubFoobary> subFoobaries = getStaticBackendFoobaryList()?.collect { StaticMainFoobary cat -> cat.backendSortedSubFoobaries }?.flatten()
List<StaticSubFoobary> electedSubFoobaries = []
for(StaticSubFoobary it:subFoobaries)
if(it.numericId in electedSubFoobaryIds)
electedSubFoobaries.add(it)
return electedSubFoobaries
}
Doesn't!
but the concrete body-loop says the current element is null
No it doesn't. If you run this code in the Groovy console, the assertion passes:
List cs = ["a"];
for(String c:cs){
assert c == 'a'
}
Conversely, if you run this code in the console the assertion fails
List cs = ["a"];
for(String c:cs){
assert c == null
}
Which conclusively proves that the first element of the list is "a" and not null
Trying to cast the GroovyString to String could be the problem. Try with a single quoted string literal.
I Finally checked out the project again and build it like i always did before.
The problem is gone away, not sure why but it works now!
I just have to copy all my changes to the new workspace and continue development.
Let this workaround be part of experiences.