所以我有多个要累加的json数据,因此我尝试了循环操作,但仍然无法正常工作,尝试了reduce仍然不工作,我将json数据的变量放入数组中但仍然无法进行操作得到结果
这是Json的数据:
[
{
"projects_id": 13,
"projects": "Immobi Track",
"assignee_id": "193",
"assignee": "Ivander",
"counts": 8
},
{
"projects_id": 13,
"projects": "Immobi Track",
"assignee_id": "195",
"assignee": "Adrian ",
"counts": 3
},
{
"projects_id": 13,
"projects": "Immobi Track",
"assignee_id": "204",
"assignee": "khalid",
"counts": 11
},
{
"projects_id": 13,
"projects": "Immobi Track",
"assignee_id": "164",
"assignee": "Testing",
"counts": 0
},
{
"projects_id": 13,
"projects": "Immobi Track",
"assignee_id": "171",
"assignee": "Ahma",
"counts": 0
},
{
"projects_id": 13,
"projects": "Immobi Track",
"assignee_id": "13",
"assignee": "Hafi",
"counts": 0
},
{
"projects_id": 13,
"projects": "Immobi Track",
"assignee_id": "17",
"assignee": "Bob ",
"counts": 0
},
{
"projects_id": 13,
"projects": "Immobi Track",
"assignee_id": "10",
"assignee": "Hest",
"counts": 0
}
]
我尝试对每个受让人数据中的“计数”数据求和,但是我无法弄清楚原因,当我尝试打印它看起来像是单个数据而不是数组内,而是将所有这些数据放入时放入数组.....这里是我的代码
struct pivotProjectSum : Codable {
let projects_id: Int
let projects : String
let assignee_id : String
let assignee : String
let counts : Int
}
private var pivot : [pivotProjectSum] = []
URLSession.shared.dataTask(with: JsonUrl) { (data, response, error) in
guard let data = data else {return}
do{
let parsing = try JSONDecoder().decode([pivotProjectSum].self, from: data)
self.pivot = parsing
DispatchQueue.main.async {
if parsing.isEmpty{
let action = UIAlertAction(title: "Ok", style: .default, handler: nil)
let alert = UIAlertController(title: "Data Empty", message: "No Data...", preferredStyle: .alert)
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
}else{
self.pivotTableView.reloadData()
}
}
}catch{
print("This is the error??♂️\(error)")
}
}.resume()
}
}
extension PicSummaryViewController : UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return pivot.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let piv = pivot[indexPath.row]
//let pivots = pivot[indexPath.self]
guard let cell = pivotTableView.dequeueReusableCell(withIdentifier: "Pivot") as? PicSummaryTableViewCell else {return UITableViewCell()}
cell.UserLbl.text = piv.assignee
cell.openLbl.text = "\(piv.counts)"
projectLbl.text = piv.projects
print(piv.counts)
let numbers = [piv.counts]
var sum = 0
var counter = 0
print("This is the numbers??♂️\(numbers)")
while counter < numbers.count{
var newValue = numbers[counter]
sum += newValue
counter += 1
}
print("this is the result ?\(counter)")
let total = numbers.reduce(0, +)
print("this is the result ?\(total)")
for i in 0...numbers.count{
sum += i
}
print("this is the result ?\(sum)")
return cell
}
//i try every single method, but i still cant get the result.
// and no im not combine all of that sum method, i use it 1 method at a time
每种方法的结果都是这样的:
This is the numbers??♂️[0]
this is the result ?1
0
This is the numbers??♂️[0]
this is the result ?1
0
This is the numbers??♂️[0]
this is the result ?1
8
This is the numbers??♂️[8]
this is the result ?1
3
This is the numbers??♂️[3]
this is the result ?1
11
This is the numbers??♂️[11]
this is the result ?1
0
This is the numbers??♂️[0]
this is the result ?1
0
This is the numbers??♂️[0]
this is the result ?1
0
This is the numbers??♂️[0]
this is the result ?1
0
This is the numbers??♂️[0]
this is the result ?1
先生,您有什么帮助吗?我仍然很新,可以在这个Swift开发人员中学习
答案 0 :(得分:1)
尝试一下;
let sum = pivot.map({ $0.counts }).reduce(0, +)
答案 1 :(得分:0)
您可以尝试
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>
<properties>
<java-version>1.6</java-version>
<org.springframework-version>4.0.3.RELEASE</org.springframework-version>
<org.aspectj-version>1.7.4</org.aspectj-version>
<org.slf4j-version>1.7.5</org.slf4j-version>
<hibernate.version>4.3.5.Final</hibernate.version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- Apache Commons DBCP -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<!-- Spring ORM -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
<!-- @Inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<additionalProjectnatures>
<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
</additionalProjectnatures>
<additionalBuildcommands>
<buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
</additionalBuildcommands>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>org.test.int1.Main</mainClass>
</configuration>
</plugin>
</plugins>
<finalName>${project.artifactId}</finalName>
</build>
</project>